Android改变选中字体颜色

目标:选中item,其字体设置为#3197FF,未选中的,其字体为#FFFFFF。其效果图如下:

bg

与listvew设置选中行item背景图片一样,使用selector,不过这里的颜色设置,应该是在listview中的textview中设置。

<?xml version="1.0" encoding="utf-8"?>  
<TableLayout
	xmlns:android="http://schemas.android.com/apk/res/android"    
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	> 
         <TableRow >
         <TextView
            android:id="@+id/name_tv"  
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:textSize="25px"
            android:textColor="@drawable/itemcolor">  
         </TextView> 
         </TableRow> 
</TableLayout>

同样,定义itemcolor.xml文件,修改选中行的字体颜色:

<?xml version="1.0" encoding="utf-8" ?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android">  
<!-- 没有焦点时字体颜色 --> 
<item
	android:state_selected="false"
	android:color="#FFFFFF"/>
<!--选中时的字体颜色  -->  
<item
	android:state_selected="true"
	android:color="#3197FF"/>
<!-- 非触摸模式下获得焦点并单击时的字体颜色 -->  
<item
	android:state_focused="true"
	android:state_pressed="true"
	android:color="#3197FF"/>
</selector>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
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` 的字体样式为正常。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值