Android 为什么我的图层列表可绘制比例不适合我的按钮?
Android why won't my layer-list drawable scale to fit my button?
我正在尝试为我的 android 按钮创建一个精美的渐变边框。我创建了一个带有图层列表和一些渐变的可绘制对象,预览看起来不错。但是当我尝试将该可绘制对象应用到我的按钮时,它看起来与预览中的样子并不接近。我希望 drawable 可以拉伸和缩放以适合我的按钮,但它似乎并没有这样做。
谁能告诉我我做错了什么?
这是我的可绘制对象:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<corners android:radius="1dp"/>
<solid android:color="@color/colorLightGray"/>
<size android:width="40dp" android:height="10dp"/>
</shape>
</item>
<item android:left="38dp">
<shape android:shape="rectangle">
<corners
android:topRightRadius="1dp"
android:bottomRightRadius="1dp"/>
<gradient
android:endColor="@color/colorDarkGreen"
android:startColor="@color/colorLightGray"
android:type="linear"/>
</shape>
</item>
<item android:right="38dp">
<shape android:shape="rectangle">
<corners
android:topLeftRadius="1dp"
android:bottomLeftRadius="1dp"/>
<gradient
android:endColor="@color/colorLightGray"
android:startColor="@color/colorDarkGreen"
android:type="linear"/>
</shape>
</item>
<item android:bottom="8dp">
<shape android:shape="rectangle">
<corners
android:topLeftRadius="1dp"
android:topRightRadius="1dp"/>
<gradient
android:endColor="@color/colorLightGray"
android:startColor="@color/colorDarkGreen"
android:angle="270"
android:type="linear"/>
</shape>
</item>
<item android:top="8dp">
<shape android:shape="rectangle">
<corners
android:bottomLeftRadius="1dp"
android:bottomRightRadius="1dp"/>
<gradient
android:endColor="@color/colorLightGray"
android:startColor="@color/colorDarkGreen"
android:angle="90"
android:type="linear"/>
</shape>
</item>
<item
android:bottom="8dp"
android:right="38dp">
<shape android:shape="rectangle">
<corners android:topLeftRadius="1dp"/>
<gradient
android:endColor="@color/colorDarkGreen"
android:centerColor="@color/colorDarkGreen"
android:startColor="@color/colorLightGray"
android:centerX="100%"
android:centerY="100%"
android:gradientRadius="200%"
android:type="radial"/>
</shape>
</item>
<item
android:bottom="8dp"
android:left="38dp">
<shape android:shape="rectangle">
<corners android:topRightRadius="1dp"/>
<gradient
android:endColor="@color/colorDarkGreen"
android:centerColor="@color/colorDarkGreen"
android:startColor="@color/colorLightGray"
android:centerX="0%"
android:centerY="100%"
android:gradientRadius="200%"
android:type="radial"/>
</shape>
</item>
<item
android:top="8dp"
android:left="38dp">
<shape android:shape="rectangle">
<corners android:bottomRightRadius="1dp"/>
<gradient
android:endColor="@color/colorDarkGreen"
android:centerColor="@color/colorDarkGreen"
android:startColor="@color/colorLightGray"
android:centerX="0%"
android:centerY="0%"
android:gradientRadius="200%"
android:type="radial"/>
</shape>
</item>
<item
android:top="8dp"
android:right="38dp">
<shape android:shape="rectangle">
<corners android:bottomLeftRadius="1dp"/>
<gradient
android:endColor="@color/colorDarkGreen"
android:centerColor="@color/colorDarkGreen"
android:startColor="@color/colorLightGray"
android:centerX="100%"
android:centerY="0%"
android:gradientRadius="200%"
android:type="radial"/>
</shape>
</item>
</layer-list>
这是我的按钮 xml:
<Button
android:id="@+id/add_some_stuff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/gradient_border"
android:textAllCaps="false"
android:textStyle="normal"
android:textSize="@dimen/settings_text_size"
android:textColor="@color/colorSettingsText"
android:text="@string/add_some_stuff"/>
预览显示如下:
这是我实际看到的:
您的可绘制对象大小是静态定义的,当它是视图的背景时将无法正确调整大小以进行调整。图层列表属性 android:width
、android:height
、android:left
等都必须使用特定的密度像素大小进行定义。一旦您的视图不再对应于该尺寸,它就无法正确适合可绘制对象(当您使用 match_parent
或 wrap_content
时会发生这种情况)。如果您必须使用动态调整大小(我建议这样做),那么我会使用矢量图像而不是 layer-list
.
我正在尝试为我的 android 按钮创建一个精美的渐变边框。我创建了一个带有图层列表和一些渐变的可绘制对象,预览看起来不错。但是当我尝试将该可绘制对象应用到我的按钮时,它看起来与预览中的样子并不接近。我希望 drawable 可以拉伸和缩放以适合我的按钮,但它似乎并没有这样做。
谁能告诉我我做错了什么?
这是我的可绘制对象:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<corners android:radius="1dp"/>
<solid android:color="@color/colorLightGray"/>
<size android:width="40dp" android:height="10dp"/>
</shape>
</item>
<item android:left="38dp">
<shape android:shape="rectangle">
<corners
android:topRightRadius="1dp"
android:bottomRightRadius="1dp"/>
<gradient
android:endColor="@color/colorDarkGreen"
android:startColor="@color/colorLightGray"
android:type="linear"/>
</shape>
</item>
<item android:right="38dp">
<shape android:shape="rectangle">
<corners
android:topLeftRadius="1dp"
android:bottomLeftRadius="1dp"/>
<gradient
android:endColor="@color/colorLightGray"
android:startColor="@color/colorDarkGreen"
android:type="linear"/>
</shape>
</item>
<item android:bottom="8dp">
<shape android:shape="rectangle">
<corners
android:topLeftRadius="1dp"
android:topRightRadius="1dp"/>
<gradient
android:endColor="@color/colorLightGray"
android:startColor="@color/colorDarkGreen"
android:angle="270"
android:type="linear"/>
</shape>
</item>
<item android:top="8dp">
<shape android:shape="rectangle">
<corners
android:bottomLeftRadius="1dp"
android:bottomRightRadius="1dp"/>
<gradient
android:endColor="@color/colorLightGray"
android:startColor="@color/colorDarkGreen"
android:angle="90"
android:type="linear"/>
</shape>
</item>
<item
android:bottom="8dp"
android:right="38dp">
<shape android:shape="rectangle">
<corners android:topLeftRadius="1dp"/>
<gradient
android:endColor="@color/colorDarkGreen"
android:centerColor="@color/colorDarkGreen"
android:startColor="@color/colorLightGray"
android:centerX="100%"
android:centerY="100%"
android:gradientRadius="200%"
android:type="radial"/>
</shape>
</item>
<item
android:bottom="8dp"
android:left="38dp">
<shape android:shape="rectangle">
<corners android:topRightRadius="1dp"/>
<gradient
android:endColor="@color/colorDarkGreen"
android:centerColor="@color/colorDarkGreen"
android:startColor="@color/colorLightGray"
android:centerX="0%"
android:centerY="100%"
android:gradientRadius="200%"
android:type="radial"/>
</shape>
</item>
<item
android:top="8dp"
android:left="38dp">
<shape android:shape="rectangle">
<corners android:bottomRightRadius="1dp"/>
<gradient
android:endColor="@color/colorDarkGreen"
android:centerColor="@color/colorDarkGreen"
android:startColor="@color/colorLightGray"
android:centerX="0%"
android:centerY="0%"
android:gradientRadius="200%"
android:type="radial"/>
</shape>
</item>
<item
android:top="8dp"
android:right="38dp">
<shape android:shape="rectangle">
<corners android:bottomLeftRadius="1dp"/>
<gradient
android:endColor="@color/colorDarkGreen"
android:centerColor="@color/colorDarkGreen"
android:startColor="@color/colorLightGray"
android:centerX="100%"
android:centerY="0%"
android:gradientRadius="200%"
android:type="radial"/>
</shape>
</item>
</layer-list>
这是我的按钮 xml:
<Button
android:id="@+id/add_some_stuff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/gradient_border"
android:textAllCaps="false"
android:textStyle="normal"
android:textSize="@dimen/settings_text_size"
android:textColor="@color/colorSettingsText"
android:text="@string/add_some_stuff"/>
预览显示如下:
这是我实际看到的:
您的可绘制对象大小是静态定义的,当它是视图的背景时将无法正确调整大小以进行调整。图层列表属性 android:width
、android:height
、android:left
等都必须使用特定的密度像素大小进行定义。一旦您的视图不再对应于该尺寸,它就无法正确适合可绘制对象(当您使用 match_parent
或 wrap_content
时会发生这种情况)。如果您必须使用动态调整大小(我建议这样做),那么我会使用矢量图像而不是 layer-list
.