如何更改android.support.design.widget.TabLayout的文本颜色和样式?

时间:2015-10-01 15:49:12

标签: android android-tablayout

如何更改标签布局的文字颜色和样式?

<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:background="#2096f3"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

1 个答案:

答案 0 :(得分:0)

您可以扩展TabLayout以提供自己的标签视图。

public class CustomTabLayout extends TabLayout {

public CustomTabLayout (final Context context) {
    super(context);
}

public CustomTabLayout (final Context context, final AttributeSet attrs) {
    super(context, attrs);
}

public CustomTabLayout (final Context context, final AttributeSet attrs, final int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

public void setTabsFromPagerAdapter(@NonNull final PagerAdapter adapter) {
    removeAllTabs();
    int i = 0;

    final LayoutInflater inflater = LayoutInflater.from(getContext());
    for(int count = adapter.getCount(); i < count; ++i) {
        final TextView tab = (TextView) inflater.inflate(R.layout.custom_tab, this, false);
        tab.setText(local.getPageTitle(i));            
        this.addTab(this.newTab().setCustomView(tab));
    }
}

}