我一直在寻找在单个shapes.xml中定义不同形状的可能性,并在某些特定事件中引用每个形状.
最后我找到了解决问题的方法.答案是使用级别列表.
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:maxLevel="0">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#aaa"
android:endColor="#eee" android:angle="270" />
<corners android:bottomrighTradius="7dp"
android:bottomLefTradius="7dp" android:topLefTradius="7dp"
android:topRighTradius="7dp" />
</shape>
</item>
<item android:maxLevel="1">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<gradient android:startColor="#eee" android:centerColor="#ddd"
android:endColor="#00fff2" android:angle="270" />
<corners android:bottomrighTradius="7dp"
android:bottomLefTradius="7dp" android:topLefTradius="7dp"
android:topRighTradius="7dp" />
</shape>
</item>
</level-list>
将其应用于样式中的background属性.通过将级别设置为该元素,可以实现不同形状的交换.
例如:findViewById(R.id.mybutton).getBackground().setLevel(1);
在上面的代码中,我将第二个形状设置为id为mybutton的按钮.
解决方法
<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="1dip" android:color="#FF8000" />
<solid
android:color="#00FFFFFF"
android:paddingLeft="10dip"
android:paddingTop="10dip"/>
<corners android:radius="10px"/>
<padding
android:left="10dip"
android:top="10dip"
android:right="10dip"
android:bottom="10dip" />
</shape>
你可以将它用于寄宿生和任何形状..以供参考……”如果它有用,那么接受答案并投票给答案