以TextView为例:
1.设置TextView背景的selector如下
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/es_shape_corner_14_ffff6a82" android:state_selected="true" />
<item android:drawable="@drawable/es_shape_corner_14_ffffff" android:state_enabled="true"/>
<item android:drawable="@drawable/es_shape_cornerleft8_cornerright8_fffcf5ff" android:state_enabled="false" />
</selector>
2.设置TextView字体颜色的selector如下
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#CCCCCC" android:state_enabled="false" />
<item android:color="#ffffff" android:state_selected="true" />
<item android:color="#666666" android:state_enabled="true"/>
</selector>
3.xml布局文件中声明TextView
<TextView
android:id="@+id/tv_vs_five_minutes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:background="@drawable/vs_selector_date_host_btn"
android:paddingLeft="8dp"
android:paddingTop="6dp"
android:paddingRight="8dp"
android:paddingBottom="6dp"
android:text="5分钟"
android:textColor="@drawable/vs_selector_date_host_btn_text"
android:textSize="12dp"
app:layout_constraintBottom_toBottomOf="@+id/tv_vs_three_minutes"
app:layout_constraintStart_toEndOf="@+id/tv_vs_three_minutes" />
4.调用方式
tv.setEnabled(true);
// tv.setEnabled(false);
tv.setSelected(true);
// tv.setSelected(false);
错误不足之处或相关建议欢迎大家评论指出,谢谢!如果觉得内容可以的话麻烦喜欢(♥)一下