是时候丢掉项目里Shape XML文件了

2022-04-10  本文已影响0人  xiaolei123

项目里的shape.xml selector.xml layer_list.xml 文件太多啦

想个办法替代吧:

1. shape.xml 的替代法

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#f00000"/>
    <corners android:radius="15dp"/>
</shape>
textView.background = shape {
    shape = RECTANGLE
    solid { color = Color.parseColor("#f00000") }
    corners { radius = 15f.dp }
}

xml 里所有的属性都支持

2. selector.xml 的替代法

normal.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#f00000"/>
    <corners android:radius="15dp"/>
</shape>

pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#00f000"/>
    <corners android:radius="15dp"/>
</shape>

selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/normal" android:state_pressed="false"/>
</selector>
textView.background = selector {
    item {
        drawable = shape {
            shape = RECTANGLE
            solid { color = Color.parseColor("#f00000") }
            corners { radius = 15f.dp }
        }
        state_pressed = false
    }
    item {
        drawable = shape {
            shape = RECTANGLE
            solid { color = Color.parseColor("#00f000") }
            corners { radius = 15f.dp }
        }
        state_pressed = true
    }
}

3. layer_list.xml 的替代法

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#C15454" />
            <corners android:radius="15dp" />
        </shape>
    </item>
    <item android:top="50dp">
        <shape android:shape="rectangle">
            <solid android:color="#155473" />
            <corners
                android:bottomLeftRadius="15dp"
                android:bottomRightRadius="15dp" />
        </shape>
    </item>
</layer-list>
layerList {
   item {
       drawable = shape {
           solid { color = Color.parseColor("#C15454") }
           corners { radius = 15f.dp }
       }
   }
   item {
       top = 50.dp
       drawable = shape {
           solid { color = Color.parseColor("#155473") }
           corners {
               leftBottomRadius = 15f.dp
               rightBottomRadius = 15f.dp
           }
       }
   }
}

4. selector_color.xml 颜色选择的替代法

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/green" android:state_pressed="true"/>
    <item android:drawable="@color/white" android:state_pressed="false"/>
</selector>
textView.setTextColor(colorSelector {
     item {
         state_pressed = true
         color = Color.WHITE
     }
     item {
         state_pressed = false
         color = Color.GREEN
     }
 })

5. todo

接下来准备实现 bitmap.xml
...

最后上链接
https://gitee.com/xcode_xiao/DropShapeXML
感兴趣的+> 559259945 加Q群交流

上一篇下一篇

猜你喜欢

热点阅读