android中shapes.xml内的多个形状
我一直在寻找在单个 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>
将其应用于样式中的背景属性。通过设置该元素的级别可以实现不同形状的互换。
例如: findViewById(R.id.mybutton).getBackground().setLevel(1);
在上面的代码中,我将第二个形状设置为 id mybutton 的按钮。
I have been searching for possibilities to define different shapes inside a single shapes.xml and refer to each one on some specific events.
At last I've found a solution to my question. And the answer is using level-list.
<?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>
Apply this to the background attribute in the style. the Interchanging of differents shapes can be achieved by setting the level to that element.
Eg: findViewById(R.id.mybutton).getBackground().setLevel(1);
In the above code I'm setting the second shape to the button with id mybutton.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将其用于寄宿生和任何形状..其可供参考...''
如果有用,请接受答案并投票赞成答案
you can use this for Boarder and any shape ..its for reference...''
If it is useful so accept the answer and vote up the answer