1.機能:
通常、さまざまな状態での色の変更または背景の変更に使用されます。
2. ステータスの概要:
android:state_selected: 選択時の状態
android:state_focused: フォーカスを取得したときの状態
android:state_pressed: 押されたときの状態
android:state_enabled: コントロールがタッチまたはクリック イベントの状態を処理できるかどうか
android:state_active: アクティブ状態
android:state_checkable: ステータスを確認できるかどうか
android:state_checked: 選択されたときの状態
android:state_hovered: カーソルが特定のコントロールに移動したときの状態
android:state_window_focused: 現在のインターフェースがフォーカスされているかどうか
3. 使用方法:
(1) まず、xml ファイルを定義します。ここでは、ファイルが次のように定義された selector_color.xml であると仮定します。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/black" android:state_checked="false" />
<item android:color="@color/white" android:state_checked="true" />
</selector>
(2) 使用方法: 対応する XML ファイルをインポートするだけです
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activity.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文字"
android:textColor="@drawable/selector_color"
android:textSize="40sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
注: これは単なるデモです。インポートした selector_color.xml をドローアブルに無造作に配置できます。より標準化する場合は、カラー フォルダーを作成し、その中に色に関するセレクターを置き、その中にドローアブルに関するセレクターを置きますドローアブルフォルダーの下にあります。