【问题标题】:How to Change font of Text in MaterialTabHost in Android?如何在 Android 中更改 MaterialTabHost 中的文本字体?
【发布时间】:2016-03-14 13:14:10
【问题描述】:

我正在使用 MaterialTabHost 来显示选项卡。我想在 onTabSelected 时更改 MaterialTabHost 的字体。我试图获得相同的文本视图。但想不通。我推荐了https://github.com/neokree/MaterialTabs。 任何帮助都会得到帮助。

这是我的 XML。

                    <!-- for Text Tabs -->
                    <it.neokree.materialtabs.MaterialTabHost
                        android:id="@+id/materialTabHost_MainActivity"
                        android:layout_width="match_parent"
                        android:layout_height="48dp"
                        android:animateLayoutChanges="false"
                        app:accentColor="@color/white"
                        app:primaryColor="@color/main_blue_light"
                        app:textColor="@color/white"

                        />

那么我们如何改变字体呢?

【问题讨论】:

    标签: android android-fragments tabs android-viewpager android-tabhost


    【解决方案1】:

    这是我在创建时将文本字体更改为选项卡的方式:

    Typeface fontRobotoRegular = Typeface.createFromAsset(getAssets(),"font/RobotoCondensed-Regular.ttf");
    MaterialTab tab = new MaterialTab(getApplicationContext(), false);
    View tabView = tab.getView();
    TextView text = (TextView) tabView.findViewById(R.id.text);
    tab.setText(pagerAdapter.getPageTitle(i));
    text.setTypeface(fontRobotoRegular);
    tab.setTabListener(this);
    tabHost.addTab(tab);
    

    因此,如果您想在选择选项卡时更改字体,您可以在 ViewPager 的 onPageSelected 上执行以下操作:

    public void onPageSelected(int position) {
        ...
        Typeface fontBold = Typeface.createFromAsset(getAssets(), "font/RobotoCondensed-Bold.ttf");
        TextView text = (TextView) tabs.getCurrentTab().getView().findViewById(R.id.text);
        text.setTypeface(fontBold);
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-08
      • 2011-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-09
      • 2014-08-12
      • 1970-01-01
      相关资源
      最近更新 更多