AndroidのコードからTabLayoutを使用して選択したタブのテキストの色を変更するにはどうすればいいですか? 質問する

AndroidのコードからTabLayoutを使用して選択したタブのテキストの色を変更するにはどうすればいいですか? 質問する

ここに画像の説明を入力してください

タブ ビューを作成するために使用していますandroid.support.widget.TabLayoutが、選択したタブのテキストの色をコードから変更したいと考えています (xml やスタイル設定ではなく)。どうすればできますか?

ベストアンサー1

XML を使用すると非常に簡単です。タブ レイアウトに次の 2 つの属性を追加するだけです。

app:tabSelectedTextColor="@color/color_primary_text"
app:tabTextColor="@color/color_secondary_text"

したがって、コードは次のようになります。

<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:layout_gravity="bottom"
    android:background="@color/button_background"
    android:fillViewport="true"
    app:tabBackground="@drawable/fixed_bottom_button"
    app:tabIndicatorColor="@color/color_primary_text"
    app:tabMode="fixed"
    app:tabSelectedTextColor="@color/color_primary_text"
    app:tabTextColor="@color/color_secondary_text" />

おすすめ記事