gpt4 book ai didi

android - 使用自定义字体更改 Tablayout 的选定选项卡颜色

转载 作者:太空宇宙 更新时间:2023-11-03 13:47:40 24 4
gpt4 key购买 nike

我正在使用 Calligraphy应用自定义字体的库。TabLayout 有问题,字体不适用。所以我必须使用以下代码手动设置它:

        mViewPager.setAdapter(pagerAdapter);
tabLayout.setupWithViewPager(mViewPager);
for (int i = 0; i < pagerAdapter.getCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
if (tab != null) {
tab.setCustomView(R.layout.tab_text);
tab.setText(pagerAdapter.getPageTitle(i));
}
}

自定义选项卡 View 的 XML 文件

  <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">

<TextView
android:id="@android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="center"
android:textSize="@dimen/item_txt_size"
android:textColor="@color/progress_color"
android:maxLines="2"
app:fontPath="fonts/font_regular.ttf"
tools:ignore="MissingPrefix"
tools:text="User Profile" />
</FrameLayout>

View 的 XML:

    <android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_below="@id/map_google"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="@color/progress_color"
app:tabIndicatorHeight="@dimen/indicator_height"
android:background="@color/actionbar_home"
app:tabMode="fixed"
app:tabTextColor="@android:color/white"
app:tabSelectedTextColor="@color/progress_color"
app:tabGravity="fill"/>

<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_below="@id/tabs"
android:layout_width="match_parent"
android:layout_height="match_parent">

</android.support.v4.view.ViewPager>

问题是选择的颜色和未选择的不适用,总是一样的颜色。

最佳答案

使用以下方法更改标签字体:

 protected void changeTabsFont(TabLayout tabLayout) {
Logger.print("In change tab font");
ViewGroup vg = (ViewGroup) tabLayout.getChildAt(0);
int tabsCount = vg.getChildCount();
Logger.print("Tab count--->"+tabsCount);
for (int j = 0; j < tabsCount; j++) {
ViewGroup vgTab = (ViewGroup) vg.getChildAt(j);
int tabChildsCount = vgTab.getChildCount();
for (int i = 0; i < tabChildsCount; i++) {
View tabViewChild = vgTab.getChildAt(i);
if (tabViewChild instanceof AppCompatTextView) {
Logger.print("In font");
Typeface type = Typeface.createFromAsset(getContext().getAssets(),"Helvetica_57_Condensed.otf");
TextView viewChild = (TextView) tabViewChild;
viewChild.setTypeface(type);
viewChild.setAllCaps(false);
}
}
}
}

要更改选项卡文本颜色,请在 styles.xml

中定义以下样式
 <style name="tab_layout">
<item name="android:background">@color/background_blue</item>
<item name="tabTextColor">@color/tab_unselected</item>
<item name="tabSelectedTextColor">@color/white</item>
<item name="android:actionBarDivider">@color/background_blue</item>
<item name="tabTextAppearance">?android:textAppearanceMedium</item>
<item name="tabIndicatorHeight">4dp</item>
</style>

并将此样式设置为您的选项卡布局:

<android.support.design.widget.TabLayout
android:id="@+id/tabs_profile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
style="@style/tab_layout"
app:tabGravity="fill"/>

关于android - 使用自定义字体更改 Tablayout 的选定选项卡颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39251650/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com