一種是在layout中使用textview,然後再代碼中設置字體顏色改變,我這裏寫了個工具類,以供參考。
public class TextcolorUtil {
public static void setTextColor(Context context, int index, TextView... textViews) {
for (int i = 0; i < textViews.length; i++) {
if (i == index) {
textViews[i].setTextColor(context.getResources().getColor(R.color.main_buttomtext_yellow));
} else {
textViews[i].setTextColor(context.getResources().getColor(R.color.main_buttomtext_grey));
}
}
}
}
使用時如下:
TextcolorUtil.setTextColor(getActivity(),3,tv1,tv2,tv3,tv4);
還有一種更簡單的方式,直接在layout中使用單選框radiobutton,設置textcolor選擇器 <RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:textSize="10sp"
android:text="@string/booklist"
android:button="@null"
android:textColor="@drawable/selector_text_color" />
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/main_buttomtext_yellow" android:state_checked="true"/>
<item android:color="@color/main_buttomtext_grey" android:state_checked="false" />
</selector>
推薦第二種方式