问题概述
UED给开发ui设计稿,这个时候,如果需要给单选控件RadioButton换图标,你知道需要ui给你什么切图吗?
查看一个控件的默认(系统)样式设置的方法
以RadioButton控件来说明
1. 查看RadioButton的两个参数的构造方法,可以看到默认样式com.android.internal.R.attr.radioButtonStyle
public RadioButton(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.radioButtonStyle);
}
2. 直接点击radioButtonStyle链接不过去,只能找文件了,可以从com.android.internal.R.attr.radioButtonStyle看出,是value资源文件夹中的attrs.xml文件中
项目的sdk的res中
res中的vlues文件夹中的attrs.xml文件可看到定义的
//values文件夹下attrs.xml文件
3. 查看主题themes.xml中的radioButtonStyle属性
//values文件夹下themes.xml文件
@style/Widget.CompoundButton.RadioButton
点击@style/Widget.CompoundButton.RadioButton可以看到
//styles.xml文件
点击?attr/listChoiceIndicatorSingle可以看到
//values文件夹下attrs.xml文件
又回到attrs.xml,继续在themes.xml中搜索listChoiceIndicatorSingle,可以看到
//values文件夹下themes.xml文件
@drawable/btn_radio
终于找到源头了
//drawable文件夹下btn_radio.xml文件
android:drawable="@drawable/btn_radio_on" />
android:drawable="@drawable/btn_radio_off" />
android:drawable="@drawable/btn_radio_on_pressed" />
android:drawable="@drawable/btn_radio_off_pressed" />
android:drawable="@drawable/btn_radio_on_selected" />
android:drawable="@drawable/btn_radio_off_selected" />
上面定义的状态很精细,而一般只需要选中和未选中状态。如下定义一个btn_radio.xml文件
//在自己的drawable文件夹下定义btn_radio.xml文件
点击btn_radio_off可以看到对应的图片资源,如下图
总结一下:RadioButton控件是由主题中的radioButtonStyle属性控制,而 radioButtonStyle中是设置了控件的button属性,button属性间接引用listChoiceIndicatorSingle属性。这样可以知道RadioButton的按钮图标控件直接由button属性控制,间接的与主题中的radioButtonStyle和listChoiceIndicatorSingle相关,下面举例几种使用方法
RadioButton中直接修改android:button属性
//直接引用上面的btn_radio
android:button="@drawable/btn_radio""
…… />
styles.xml中定义样式,themes.xml中引用使用或者RadioButton引用使用
//styles.xml中定义样式
//themes.xml中引用使用
@style/RadioButtonStyle
//RadioButton引用使用
…… />
修改themes.xml中listChoiceIndicatorSingle属性(不推荐这种方式,listChoiceIndicatorSingle还被其他控件的属性引用,可能会造成不需要的修改效果)
//修改themes.xml中listChoiceIndicatorSingle属性
@drawable/btn_radio
此文要是对你有帮助,如果方便麻烦点个赞,谢谢!!!
本文地址:https://blog.csdn.net/kingyc123456789/article/details/107590740