Android 主题和选择器

今天在做底部tab的时候因为样式都一样 所以就自定义一个style 这样省的写很多重复的样式(懒懒懒懒), 修改的话直接在样式里修改省去一个一个修改一样的代码

1 在values/styles.xml 里添加 radiobutton样式:

 

    <style name="BottomTabStyle">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_weight">1</item>
        <item name="android:button">@null</item>
        <item name="android:gravity">center</item>
        <item name="android:padding">5dp</item>
        <item name="android:drawablePadding">3dp</item>
        <item name="android:textColor">@color/txt_bottom_tab_selector</item>
    </style>

 

2 颜色选择器再res/color里 添加txt_bottom_tab_selector.xml:

 

 

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="true" android:color="#f00"></item>//state_checked是选择后 state_pressed是按下 <item android:color="#fff"></item> //默认 </selector>

 

 3 图片选择器:res/drawable下

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/home_press" android:state_checked="true"></item>
    <item android:drawable="@drawable/home"></item>

</selector>

 

4 再layout中 用style来引入主题样式: drawabletop @drawable 引入图片选择器

       <RadioButton
            android:id="@+id/rb_home"
            style="@style/BottomTabStyle"
            android:drawableTop="@drawable/btn_home_selector"
            android:checked="true"
            android:text="首页" />

 

 

原文地址:https://www.cnblogs.com/AceIsSunshineRain/p/5185167.html