RadioButton设置selector颜色和图片选择器

  今天使用RadioButton的时候,遇到按钮设置颜色和图片选择器无效的问题,网上也找了好久也弄不好。其实就是state_checked的问题,记得以前使用button的时候使用state_selected是可以的,不知道RadioButton为什么不行。今天把它记下来,以免大家也犯同样的错误!

1、首先来看selector文件,这是图片的

<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.这是设置文字颜色的

<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.这是RadioButton的布局和style文件,checked="true"是默认选中的按钮

<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>
————————————————

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 改变字体大小 可以通过在 `onCheckedChanged` 方法中设置 `TextView` 的字体大小来实现。具体步骤如下: 1. 在布局文件中添加一个 `RadioButton` 和一个 `TextView`。 ```xml <RadioGroup android:id="@+id/radio_group" android:layout_width="match_parent" android:layout_height="wrap_content"> <RadioButton android:id="@+id/radio_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RadioButton"/> </RadioGroup> <TextView android:id="@+id/text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello World!"/> ``` 2. 在 Activity 中获取 `RadioButton` 和 `TextView` 的实例,并设置 `onCheckedChangeListener`。 ```java RadioButton radioButton = findViewById(R.id.radio_button); TextView textView = findViewById(R.id.text_view); radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { textView.setTextSize(24); } else { textView.setTextSize(16); } } }); ``` 当 `RadioButton` 被选中时,设置 `TextView` 的字体大小为 24sp;当 `RadioButton` 没有被选中时,设置 `TextView` 的字体大小为 16sp。 2. 改变字体颜色 可以通过在 `onCheckedChanged` 方法中设置 `TextView` 的字体颜色来实现。具体步骤如下: 1. 在布局文件中添加一个 `RadioButton` 和一个 `TextView`。 ```xml <RadioGroup android:id="@+id/radio_group" android:layout_width="match_parent" android:layout_height="wrap_content"> <RadioButton android:id="@+id/radio_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RadioButton"/> </RadioGroup> <TextView android:id="@+id/text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello World!"/> ``` 2. 在 Activity 中获取 `RadioButton` 和 `TextView` 的实例,并设置 `onCheckedChangeListener`。 ```java RadioButton radioButton = findViewById(R.id.radio_button); TextView textView = findViewById(R.id.text_view); radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { textView.setTextColor(Color.RED); } else { textView.setTextColor(Color.BLACK); } } }); ``` 当 `RadioButton` 被选中时,设置 `TextView` 的字体颜色为红色;当 `RadioButton` 没有被选中时,设置 `TextView` 的字体颜色为黑色。 3. 改变字体加粗 可以通过在 `onCheckedChanged` 方法中设置 `TextView` 的字体样式来实现。具体步骤如下: 1. 在布局文件中添加一个 `RadioButton` 和一个 `TextView`。 ```xml <RadioGroup android:id="@+id/radio_group" android:layout_width="match_parent" android:layout_height="wrap_content"> <RadioButton android:id="@+id/radio_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RadioButton"/> </RadioGroup> <TextView android:id="@+id/text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello World!"/> ``` 2. 在 Activity 中获取 `RadioButton` 和 `TextView` 的实例,并设置 `onCheckedChangeListener`。 ```java RadioButton radioButton = findViewById(R.id.radio_button); TextView textView = findViewById(R.id.text_view); radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD)); } else { textView.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL)); } } }); ``` 当 `RadioButton` 被选中时,设置 `TextView` 的字体样式为加粗;当 `RadioButton` 没有被选中时,设置 `TextView` 的字体样式为正常。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值