When using RadioButton today, I encountered the problem that the button setting color and the picture selector were invalid. In fact, it is the problem of state_checked. I remember that it is possible to use state_selected when using button before. I don’t know why RadioButton can’t. Jot it down today before everyone else makes the same mistake!
1. First look at the selector file, which is the picture
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/main_3d_green" android:state_checked="true" /> <item android:drawable="@drawable/main_3d" /> </selector>
2. This is to set the text color
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="@color/colorGreenPrimary" android:state_checked="true" /> <item android:color="@color/color_80" /> </selector>
3. This is the layout and style file of RadioButton, checked="true" is the default selected button
<RadioButton android:id="@+id/rBtn_3d" style="@style/main_radioButton_style" android:drawableTop="@drawable/select_3d_btn_img" android:text="3D相册" android:textColor="@drawable/select_main_tv_color" android:checked="true"/>
<style name="main_radioButton_style"> <item name="android:layout_width">0dp</item> <item name="android:layout_height">match_parent</item> <item name="android:layout_weight">1</item> <item name="android:gravity">center</item> <item name="android:textSize">14sp</item> <item name="android:paddingTop">3dp</item> <item name="android:paddingEnd">3dp</item> <item name="android:button">@null</item> </style>
4. Finally insert a toolbar title color setting:
Because the font of the toolbar is black by default, it is ugly; so we change it to white, where textColorPrimary is the key. A lot of people on Baidu say to use " actionMenuTextColor " without adding the android prefix, all of which will not work! It is estimated that it is a question of Level!
<style name="toolbarTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:textColorPrimary">@color/color_white</item> <item name="android:textSize">18sp</item> </style>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="@color/colorGreenPrimary" android:theme="@style/toolbarTheme"> </android.support.v7.widget.Toolbar>