繁体   English   中英

更改在 Android Studio 中选择的 TabLayout 的颜色

[英]Change color of TabLayout selected in Android Studio

我是 Android Studio 的新手,在此页面上,我需要知道如何在选择选项卡时更改颜色。

我已经尝试过设计,但没有成功。 这是代码:

android:id="@+id/tab_layout_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/logo_home"
android:background="@null"
android:visibility="visible"
app:layout_scrollFlags="enterAlways"
app:tabGravity="center"
app:tabPaddingBottom="0dp"
app:tabPaddingEnd="@dimen/content_inset_half"
app:tabPaddingStart="@dimen/content_inset_half"
app:tabPaddingTop="0dp"
app:tabTextColor="@color/tab_text_color_selector"
app:tabSelectedTextColor="@color/colorWindowsBackgroundLight"

我假设您正在使用支持库。

1...你可以简单地在你的代码中这样做:

 tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#FF0000"));
    tabLayout.setSelectedTabIndicatorHeight((int) (5 * getResources().getDisplayMetrics().density));
    tabLayout.setTabTextColors(Color.parseColor("#727272"), Color.parseColor("#ffffff"));

2...您也可以在您的 xml 中执行此操作。 将以下内容添加到您的选项卡中

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

你在 xml 中的 tablayout 最终看起来像这样

android:id="@+id/tab_layout_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/logo_home"
android:background="@null"
android:visibility="visible"
app:layout_scrollFlags="enterAlways"
app:tabGravity="center"
app:tabPaddingBottom="0dp"
app:tabPaddingEnd="@dimen/content_inset_half"
app:tabPaddingStart="@dimen/content_inset_half"
app:tabPaddingTop="0dp"
app:tabTextColor="@color/tab_text_color_selector"
app:tabSelectedTextColor="@color/colorWindowsBackgroundLight"
app:tabSelectedTextColor="@color/color_primary_text"
app:tabTextColor="@color/color_secondary_text" />

尝试这个:

xml 代码选项卡布局

<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" />

像这样在选项卡布局中添加两个属性

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

Java代码

tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#FF0000"));
tabLayout.setSelectedTabIndicatorHeight((int) (5 * getResources().getDisplayMetrics().density));
tabLayout.setTabTextColors(Color.parseColor("#727272"), Color.parseColor("#ffffff"));

它可以帮助你

好吧,感谢您对答案的支持,我使用了很多我找到的所有信息,但问题是我没有在getTabAt()方法中使用setText() ,我使用的是setCustomView() xml 中的自定义视图,这就是为什么它没有改变颜色或tabSelectedTextColor()方法显然不起作用。 只需删除自定义设计并使用本机设计手动输入选项卡的标题,我就这样做了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM