我想用一个定制的可拉拔,使我的按钮圆角。但是,我也希望矩形的底部1/4填充一种特定的颜色。这可能吗?我怎么才能填到最下面的四分之一呢?
最佳答案
是的,如果您使用Layer List并定义重叠项,则是可能的。这里有一个例子…
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="0dp" android:left="0dp" android:bottom="0dp" android:right="0dp">
<shape android:shape="rectangle">
<corner android:radius="5"/>
<solid android:color="#f000"/>
<size android:width="200dp"
android:height="30dp"/>
</shape>
</item>
<item android:top="0dp" android:left="0dp" android:bottom="0dp" android:right="0dp">
<shape android:shape="rectangle">
<corner android:radius="5"/>
<solid android:color="#0f0"/>
<size android:width="50dp"
android:height="30dp"/>
</shape>
</item>
</layer-list>
你可能需要玩弄这些价值观,并根据你的需要调整它们
编辑
我不认为你可以指定相对值,比如百分比。但是,如果这些值因屏幕大小不同而不同,则可以(我建议)将这些值移动到维度资源文件中,如下所示…
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="button_full_width">200dp</dimen>
<dimen name="button_quarter_width">50dp</dimen>
<dimen name="button_height">30dp</dimen>
</resources>
然后在你的图纸中指定这些尺寸如下…
...
<size android:width="@dimen/button_full_width"
android:height="@dimen/button_height"/>
...
现在,要为不同的屏幕大小指定不同的值,您需要通过添加不同的“值”资源文件夹并将维度文件以不同的值放在其中来指定针对不同大小的资源限定符。android系统将在运行时根据设备屏幕大小获取并加载必要的尺寸……
res/
values/
dimens.xml
values-small/
dimens.xml
values-normal/
dimens.xml
values-large/
dimens.xml
values-xlarge/
dimens.xml
更多信息check out the documentation