TextView 字体颜色选择器结合 ViewPager 的使用

本文介绍了一个简单的Android应用程序如何通过XML资源文件实现文本颜色的选择器效果。利用`textcolor.xml`定义了不同状态下的文本颜色,并在`search.xml`布局文件中应用这些颜色。同时展示了如何在ViewPager组件中设置页面切换时文本颜色的变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.res/color/textcolor.xml

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


    <item android:state_selected="true" android:color="@color/textColor_selected"/>
    <item android:state_selected="false" android:color="@color/textColor_unselected"/>



</selector>


2.res/values/strings


<?xml version="1.0" encoding="utf-8"?>
<resources>


    <string name="app_name">Hello</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>


    <color name="textColor_selected">#FBFDFE</color>
    <color name="textColor_unselected">#7FBBEC</color>



</resources>


3.search.xml

 <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >


            <TextView
                android:id="@+id/content"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="15dp"
                android:layout_marginLeft="60dp"
                android:layout_marginTop="10dp"
                android:text="内容"
                android:textColor="@color/textcolor"
                android:textSize="25sp" />


            <TextView
                android:id="@+id/person"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginBottom="15dp"
                android:layout_marginRight="60dp"
                android:layout_marginTop="10dp"
                android:text="人"
                android:textColor="@color/textcolor"
                android:textSize="25sp" />
        </RelativeLayout>


4.在 ViewPager 选择器中设置


viewPager.setOnPageChangeListener(new OnPageChangeListener() {


@Override
public void onPageSelected(int arg0) {
// 设置滑动页显示光标
textView1.setSelected(arg0 == 0 ? true : false);
textView2.setSelected(arg0 == 1 ? true : false);

}


@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {


}


@Override
public void onPageScrollStateChanged(int arg0) {


}
});


5.效果图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值