TabLayout (Android Design Library) Text Color

后端 未结 9 914
面向向阳花
面向向阳花 2020-12-04 22:50

I\'m using the new TabLayout from the Android Design library. I managed to set the textcolor statelist using tabLayout.setTabTextColors(colorstatelist)

How can i ac

相关标签:
9条回答
  • 2020-12-04 23:43

    With the TabLayout provided in the Material Components Library you can:

    • Use a custom style
      <com.google.android.material.tabs.TabLayout
          style="@style/My_Tablayout"
          ..>
    

    and in your style use the tabTextColor with a selector.

    <!-- TabLayout -->
    <style name="My_Tablayout" parent="Widget.MaterialComponents.TabLayout" >
        <item name="tabTextColor">@color/tab_layout_selector</item>
    </style>
    
    
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:color="?attr/colorPrimary" android:state_selected="true"/>
      <item android:alpha="0.60" android:color="?attr/colorOnSurface"/>
    </selector>
    
    • Use the app:tabTextColor in your layout:
      <com.google.android.material.tabs.TabLayout
          app:tabTextColor="@color/tab_layout_selector"
          ..>
    

    0 讨论(0)
  • 2020-12-04 23:44

    Via XML attributes:

    <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="fixed"
            app:tabGravity="fill"
            app:tabTextColor="@color/your_unselected_text_color"
            app:tabSelectedTextColor="@color/your_selected_text_color"/>
    

    Additionally, there are attributes like tabIndicatorColor or tabIndicatorHeight for further styling.

    In code:

    tabLayout.setTabTextColors(
        getResources().getColor(R.color.your_unselected_text_color),
        getResources().getColor(R.color.your_selected_text_color)
    );
    

    Since this old way is deprecated as of API 23, the alternative is:

    tabLayout.setTabTextColors(
        ContextCompat.getColor(context, R.color.your_unselected_text_color),
        ContextCompat.getColor(context, R.color.your_selected_text_color)
    );
    
    0 讨论(0)
  • 2020-12-04 23:52

    best or short and simple way is make custom appbar like

     <?xml version="1.0" encoding="utf-8"?>
        <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:background="@color/colorAccent"
        app:theme="@style/myCustomAppBarTheme"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"><RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <ImageButton
                android:id="@+id/btn_back"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:background="@android:color/transparent"
                android:src="@mipmap/ic_launcher" />
    
            <TextView
                android:id="@+id/txt_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="16dp"
                android:layout_marginStart="16dp"
                android:layout_toEndOf="@+id/btn_back"
                android:layout_toRightOf="@+id/btn_back"
                android:text="Title"
                android:textColor="@android:color/white"
                android:textSize="20sp"
                android:textStyle="bold" />
    
        </RelativeLayout>
        </android.support.v7.widget.Toolbar>
    
    0 讨论(0)
提交回复
热议问题