android - TabLayout(Android 设计库)文本颜色

标签 android android-design-library androiddesignsupport

我正在使用来自 Android 设计库的新 TabLayout。我设法使用 tabLayout.setTabTextColors(colorstatelist) 设置了 textcolor statelist

如何使用styles.xml 实现相同的效果?

最佳答案

通过 XML 属性:

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

此外,还有 tabIndicatorColor 或 tabIndicatorHeight 等属性用于进一步的样式设置。

在代码中:

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

由于这种旧方式自 API 23 起已被弃用,替代方案是:

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

关于android - TabLayout(Android 设计库)文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30909471/

相关文章:

android - 选项卡图标和文本都使用 android 设计支持库

android - 当 EditText 获得焦点时,CollapsingToolbarLayout 不会折叠

bluetooth - Android连接到Arduino +蓝牙

android - 如何检测用户从哪个应用程序返回到您的应用程序?

android - 如何修复此错误?膨胀类 android.support.design.widget.NavigationView 时出错

android - CollapsingToolbarLayout 不显示自定义标题

android - TabLayout 设置每个选项卡的间距或边距

android - 具有多个条件的 SQL Android 查询公式

android - 带有 Android 数据库连接的 ORMLite

Android Bottomsheet TextView 高度在第一次扩展时未调整