我这里仅对文字的颜色进行更改
1、编辑selector文件
在drawable文件夹下创建selector_text_press_color.xml(名字任意)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/blue" android:state_checked="true" /><!--选中状态的颜色-->
<item android:color="@color/black"/>
</selector>
2、编辑布局文件
我是用RadioButton做的效果
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<RadioGroup
android:id="@+id/rg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb1"
style="@style/tab_menu_item"
android:text="@string/rb1" />
<RadioButton
android:id="@+id/rb2"
style="@style/tab_menu_item"
android:text="@string/rb2" />
<RadioButton
android:id="@+id/rb3"
style="@style/tab_menu_item"
android:text="@string/rb3" />
<RadioButton
android:id="@+id/rb4"
style="@style/tab_menu_item"
android:text="@string/rb4"/>
<RadioButton
android:id="@+id/rb5"
style="@style/tab_menu_item"
android:text="@string/rb5"/>
</RadioGroup>
</HorizontalScrollView>
配套的样式文件
<!--style.xml-->
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="tab_menu_item" parent="text1">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">@color/white</item>
<item name="android:textSize">20dp</item>
<item name="android:gravity">center</item>
<item name="android:button">@null</item> <!--去除RadioButton前面的圆点框-->
<item name="android:textColor">@drawable/selector_text_press_color</item>
<item name="android:layout_marginLeft">15dp</item>
<item name="android:layout_marginRight">15dp</item>
</style>
</resources>
3、Java代码
//activity要继承RadioGroup.OnCheckedChangeListener接口对RadioGroup进行监听,继承该接口后要重写onCheckedChanged()方法
public class MyActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {//注意要继承RadioGroup.OnCheckedChangeListener接口
private RadioGroup radioGroup;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_documents);
init();
}
private void init() {
radioGroup=findViewById(R.id.rg);
radioGroup.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.rb1:
break;
case R.id.rb2:
break;
case R.id.rb3:
break;
case R.id.rb4:
break;
case R.id.rb5:
break;
}
}
|