在android中的shapes.xml里面的Multipe形状

我一直在寻找在单个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>

你可以将它用于寄宿生和任何形状..以供参考……”如果它有用,那么接受答案并投票给答案

相关文章

ADB Remote ATV Android TV 的遥控器,基于 ADB Shell 命令 ...
使用Flutter自带的SearchDelegate组件实现搜索界面,通过魔改...
上篇文章讲解了怎么使用Kotlin的协程配合Retrofit发起网络请...
安卓开发——WebView+Recyclerview文章详情页,解决高度...
Android 如何解决dialog弹出时无法捕捉Activity的back事件 在...