在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的按钮.

@R_502_5620@

<?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>

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

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...