是時(shí)候丟掉項(xiàng)目里Shape XML文件了

項(xiàng)目里的shape.xml selector.xml layer_list.xml 文件太多啦

想個(gè)辦法替代吧:

1. shape.xml 的替代法

  • 舊寫(xiě)法 - 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>
  • 新寫(xiě)法-kotlin
textView.background = shape {
    shape = RECTANGLE
    solid { color = Color.parseColor("#f00000") }
    corners { radius = 15f.dp }
}

xml 里所有的屬性都支持

2. selector.xml 的替代法

  • 舊寫(xiě)法 - 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>
  • 新寫(xiě)法 - kotlin
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 的替代法

  • 舊寫(xiě)法 - 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>
  • 新寫(xiě)法 - kotlin
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 顏色選擇的替代法

  • 舊寫(xiě)法 - 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>
  • 新寫(xiě)法 - kotlin
textView.setTextColor(colorSelector {
     item {
         state_pressed = true
         color = Color.WHITE
     }
     item {
         state_pressed = false
         color = Color.GREEN
     }
 })

5. todo

接下來(lái)準(zhǔn)備實(shí)現(xiàn) bitmap.xml
...

End

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容