我正在尝试重复使用基本形状,并使用这些声明性的XML元素来组成我的用户界面。
如何创建可变的Android属性?
但我不想为每个属性值及其排列组合创建单独的XML文件,并在此过程中重复大部分工作。
例如,我希望该形状的使用者能够定义android:radius值?
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#449def"
android:endColor="#2f6699"
android:angle="270"/>
<stroke
android:width="1dp"
android:color="#2f6699"/>
<corners
android:radius="3dp"/>
</shape>
如何从XML父元素设置属性?
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/shape_box_round_blue_uniform" />
<!-- How to set the corner radius here? -->
<item android:drawable="@drawable/shape_box_round_blue" />
</selector>
有什么解决方案吗?
- 尽可能地不使用任何Java代码 / 避免创建自定义控件/类。
- 使用维度资源可能是一个有成效的途径?
values/dimensions.xml
中定义这个radius_round_blue。如果此属性根据某些值标识符(如语言环境或屏幕大小)而变化,则这可能特别有用。但是,为了完全自由,您应该创建Java类,但这又将增加额外的编码工作(以设置这些可绘制对象和bla)。 - Sherif elKhatib