我正在使用Android设计库中的新TabLayout。我设法使用tabLayout.setTabTextColors(colorstatelist)
设置textcolor状态列表
如何使用styles.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)
);
以下是覆盖文本样式和所选文本颜色的代码段代码
<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<item name="tabTextAppearance">@style/MyCustomTabText</item>
<item name="tabSelectedTextColor">@color/tab_text_act</item>
</style>
<style name="MyCustomTabText" parent="TextAppearance.AppCompat.Button">
<item name="android:textSize">14sp</item>
<item name="android:textColor">@color/tab_text</item>
</style>
这是布局的代码段
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/MyCustomTabLayout" />
上面的所有答案都是正确的,但我认为最好覆盖默认样式,只更改要更改的特定元素。下面的示例将使文本变为粗体:
<style name="Widget.TabItem" parent="TextAppearance.Design.Tab">
<item name="android:textStyle">bold</item>
</style>
然后..,
app:tabTextAppearance="@style/Widget.TabItem"
你只需要覆盖android:textAppearance
风格。因为TabLayout使用textAppearance。这是风格的小片段代码。
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Below will reference with our custom style -->
<item name="android:textAppearance">@style/my_tab_text</item>
</style>
<style name="my_tab_text" parent="Base.TextAppearance.AppCompat">
<item name="android:textColor">@android:color/holo_blue_dark</item>
</style>
如果您不想从Apptheme引用,可以使用以下代码段直接指定TabLayout。
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextAppearance="@style/my_tab_text"
app:tabIndicatorHeight="48dp"/>
对于自定义选项卡,我们必须覆盖以下内容:1)app:tabTextColor // for_unselected_text“
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
style="@style/MyCustomTabLayout"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="?attr/colorPrimary"
android:scrollbarSize="24sp"
android:visibility="gone"
app:tabTextColor="@color/white_40_percent"
app:tabMode="scrollable" />
2)tabSelectedTextColor //用于选定的选项卡颜色3)tabIndicatorColor //用于选项卡指示符的颜色
<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<item name="android:textColorPrimary">@color/white</item>
<item name="tabSelectedTextColor">@color/white</item>
<item name="tabTextAppearance">@style/TabTextStyle</item>
<item name="tabIndicatorColor">?attr/colorAccent</item>
<item name="tabIndicatorHeight">4dp</item>
<item name="android:tabStripEnabled">true</item>
<item name="android:padding">0dp</item>
</style>
<style name="TabTextStyle">
<item name="android:fontFamily">@string/font_fontFamily_medium</item>
<item name="android:textStyle">bold</item>
<item name="android:textAllCaps">true</item>
<item name="android:textColor">@color/tab_text_color</item>
<item name="android:textSize">16sp</item>
</style>
tab_text_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/white_40_percent"android:state_selected="false"/>
<item android:color="@color/white_100_percent"android:state_selected="true"/>
</selector>
简单而完美的方式:
在xml文件中::
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextAppearance="@style/TabText"/>
在values-styles文件中:
注意:“cairo_semibold”是外部字体,您可以将其替换为您的字体。
<style name="TabText" parent="TextAppearance.Design.Tab">
<item name="android:textColor">#1f57ff</item>
<item name="android:textSize">14sp</item>
<item name="android:fontFamily">@font/cairo_semibold</item>
</style>
最好或简短的方法是使自定义appbar像
<?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>