【问题标题】:How to Change Text Color of tab Layout?如何更改选项卡布局的文本颜色?
【发布时间】:2018-05-16 21:36:19
【问题描述】:

我有这个代码来改变标签布局的文本颜色, 但它根本不起作用!

app:tabTextColor 不起作用,它不能将颜色变为白色。

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        app:tabIndicatorColor="@color/blue"
        app:tabIndicatorHeight="5dp"
        app:tabTextColor="@color/white" />

【问题讨论】:

  • 你的背景是白色的,你希望你的 tabtextcolor 是白色的
  • 你想解释什么。你能上传你的截图吗

标签: android layout android-tablayout


【解决方案1】:
 <com.google.android.material.tabs.TabLayout
            android:id="@+id/color"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:tabTextColor="#FFFFFF"
            app:layout_constraintStart_toStartOf="parent"
            >

            <com.google.android.material.tabs.TabItem
                android:id="@+id/showcase"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TAB 1"
                />

            <com.google.android.material.tabs.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TAB 2" />

            <com.google.android.material.tabs.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TAB 3" />
        </com.google.android.material.tabs.TabLayout>

【讨论】:

    【解决方案2】:

    使用此代码将有助于所有 api 级别 api 18 到 api 26

    tabLayout.setupWithViewPager(viewPager,true);
            tabLayout.setSelected(true);
    
            tabLayout.setTabTextColors(getResources().getColor(R.color.colorHintTextLight),
                      getResources().getColor(R.color.colorPrimaryTextLight));
    
    <color name="colorHintTextLight">#80FFFFFF</color>
        <color name="colorPrimaryTextLight">#FFFFFF</color>
    

    这对你有帮助。当标签布局改变位置时它会帮助我。

    【讨论】:

    • 不错的一个!快速和ez
    【解决方案3】:

    试试看 -

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

    【讨论】:

      【解决方案4】:

      您可以自定义 TabLayout 的文本。

      像这样从 Java 代码或 XML 创建一个 TextView

      <?xml version="1.0" encoding="utf-8"?>
      <TextView
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:id="@android:id/text1"
          android:layout_width="match_parent"
          android:textSize="15sp"
          android:textColor="@color/tabs_default_color"
          android:gravity="center"
          android:layout_height="match_parent"
      />
      

      请确保保持 id 原样,因为如果您使用自定义 TextView,TabLayout 会检查此 ID

      然后从代码中扩展此布局并在该 TextView 上设置自定义字体并将此自定义视图添加到选项卡。

      for (int i = 0; i < tabLayout.getTabCount(); i++) {
          //noinspection ConstantConditions
       TextView tv=(TextView)LayoutInflater.from(this).inflate(R.layout.custom_tab,null)
       tv.setTextColor(customColor)
       tabLayout.getTabAt(i).setCustomView(tv);
      
      }
      

      【讨论】:

      • tabLayout.getTabAt(i).setCustomView(tv);它在堆栈中添加视图,有什么想法可以避免视图堆栈?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多