如何在TabLayout中将标签的标题设置为具有相同的字体大小?

时间:2017-10-03 16:37:50

标签: android android-viewpager android-tablayout android-design-library fragmentpageradapter

我正在使用FragmentPagerAdapterViewPager创建一个包含4个标签的TabLayout。除了字体大小,一切正常。

我尝试了以下主题中提出的不同解决方案,但没有任何方法对我有用:

Text size of android design TabLayout tabs

How to increase icon size of tabs in TabLayout

android font size of tabs

Change the text size in tab in android

这就是我的TabLayout目前的样子:

这些是我的相关课程:

  

MainActivity.class

private void setupUi() {
    final MainPagerAdapter mainPagerAdapter =
            new MainPagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(mainPagerAdapter);
    tabLayout.setupWithViewPager(viewPager);
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
}
  

MainPagerAdapter.class

public class MainPagerAdapter extends FragmentPagerAdapter {

    private static final String UPCOMING_TITLE = "UPCOMING";
    private static final String INTERESTS_TITLE = "INTERESTS";
    private static final String ABOUT_TITLE = "ABOUT";
    private static final String ACCOUNT_TITLE = "ACCOUNT";

    private static final Integer TAB_COUNT = 4;

    public MainPagerAdapter(final FragmentManager fragmentManager) {
        super(fragmentManager);
    }

    @Override
    public Fragment getItem(final int position) {
        switch (position) {
        case 0:
        default:
            return new UpcomingFragment();
        case 1:
            return new InterestsFragment();
        case 2:
            return new AccountFragment();
        case 3:
            return new AboutFragment();
        }
    }

    @Override
    public int getCount() {
        return TAB_COUNT;
    }

    @Override
    public CharSequence getPageTitle(final int position) {
        switch (position) {
            case 0:
                return UPCOMING_TITLE;
            case 1:
                return INTERESTS_TITLE;
            case 2:
                return ACCOUNT_TITLE;
            case 3:
                return ABOUT_TITLE;
            default:
                return null;
        }
    }
}
  

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:layout_scrollFlags="scroll|enterAlways"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

</android.support.v7.widget.Toolbar>
  

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorPrimaryDark</item>
    </style>

    <style name="AppTheme.TabLayout" parent="Widget.Design.TabLayout">
        <item name="tabIndicatorColor">?attr/colorAccent</item>
        <item name="tabIndicatorHeight">2dp</item>
        <item name="tabPaddingStart">0dp</item>
        <item name="tabPaddingTop">0dp</item>
        <item name="tabPaddingBottom">0dp</item>
        <item name="tabPaddingEnd">0dp</item>
        <item name="tabBackground">?attr/selectableItemBackground</item>
        <item name="tabSelectedTextColor">?android:textColorPrimary</item>
        <item name="android:background">@color/colorPrimary</item>
        <item name="tabTextAppearance">@style/TabLayoutTextAppearance</item>
    </style>

    <style name="TabLayoutTextAppearance" parent="TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse">
        <item name="android:textSize">14sp</item>
        <item name="android:textAllCaps">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>

</resources>
  

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true" >

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            android:theme="@style/AppTheme.AppBarOverlay">

            <include
                android:id="@+id/toolbar"
                layout="@layout/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

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

        </android.support.design.widget.AppBarLayout>

        <android.support.v4.view.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    </android.support.design.widget.CoordinatorLayout>


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view_filter"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="right|end"
        android:layout_marginTop="?attr/actionBarSize"
        android:background="@color/md_white_1000" />

</android.support.v4.widget.DrawerLayout>

请保存我!

2 个答案:

答案 0 :(得分:1)

在activity_main.xml中尝试此操作

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

答案 1 :(得分:0)

可能的选择。

应用程式:tabMode =&#34;可滚动&#34;

这使得每个标签的标题具有相同的字体大小。但与此同时,标签不再被修复。 但如果你保持&#34;固定&#34;标签模式,在宽度较小的手机中,标题看起来很小,甚至可以转到下一行。所以,tabMode =&#34;可滚动&#34;是4个标签的更好选择。

但是,如果你需要为每个标题固定标签和相同的字体大小,请设置样式的字体大小并确保它很小。 示例:

`<style name="MyTabLayoutTextAppearance" parent="TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse">
        <item name="android:textSize">13sp</item>
        <item name="android:textAllCaps">true</item> 
    </style>`