Java 如何在Android中更改所选选项卡标题文本大小

Java 如何在Android中更改所选选项卡标题文本大小,java,android,android-viewpager,styles,android-tablayout,Java,Android,Android Viewpager,Styles,Android Tablayout,我正在试图找到一种方法来更改选中时选项卡标题的文本大小。直到现在都没有出口。希望有人能帮助我 我的代码如下: XML: 还有我的旋转木马碎片类: public class CarouselFragment extends Fragment { private TabLayout tabLayout; private ViewPager viewPager; private ViewPagerAdapter viewPagerAdapter; @Nullab

我正在试图找到一种方法来更改选中时
选项卡标题的文本大小。直到现在都没有出口。希望有人能帮助我

我的代码如下:

XML:

还有我的旋转木马碎片类:

 public class CarouselFragment extends Fragment {

    private TabLayout tabLayout;
    private ViewPager viewPager;
    private ViewPagerAdapter viewPagerAdapter;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_carousel, container, false);
        tabLayout = (TabLayout)root.findViewById(R.id.tab_layout);
        viewPager = (ViewPager)root.findViewById(R.id.pager_view);
        setHasOptionsMenu(true);
        return root;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


        viewPagerAdapter = new ViewPagerAdapter(getChildFragmentManager(), getResources());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            viewPager.setAdapter(viewPagerAdapter);
            tabLayout.setSelectedTabIndicatorColor(Color.RED);
            tabLayout.setupWithViewPager(viewPager);
        }else {
            viewPager.setAdapter(viewPagerAdapter);
            tabLayout.setupWithViewPager(viewPager);
        }

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
//here i should do something, but what???

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
//here i should do something, but what???
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
}
非常感谢

使用此代码

for (int i = 0; i < tabLayout.getTabCount(); i++) {

    TabLayout.Tab tab = tabLayout.getTabAt(i);
    if (tab != null) {

        TextView tabTextView = new TextView(this);
        tab.setCustomView(tabTextView);

        tabTextView.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT;
        tabTextView.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;

        tabTextView.setText(tab.getText());

        if (i == 0) {
            tabTextView.setTextSize(16);
        }

    }

}
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        ViewGroup vg = (ViewGroup) tabLayout.getChildAt(0);
        ViewGroup vgTab = (ViewGroup) vg.getChildAt(tab.getPosition());
        int tabChildsCount = vgTab.getChildCount();
        for (int i = 0; i < tabChildsCount; i++) {
            View tabViewChild = vgTab.getChildAt(i);
            if (tabViewChild instanceof TextView) {
                ((TextView) tabViewChild).setTextSize(16);
            }
        }
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {
        ViewGroup vg = (ViewGroup) tabLayout.getChildAt(0);
        ViewGroup vgTab = (ViewGroup) vg.getChildAt(tab.getPosition());
        int tabChildsCount = vgTab.getChildCount();
        for (int i = 0; i < tabChildsCount; i++) {
            View tabViewChild = vgTab.getChildAt(i);
            if (tabViewChild instanceof TextView) {
                ((TextView) tabViewChild).setTextSize(14);
            }
        }
    }
}
for(int i=0;i
我建议设置自定义选项卡

首先,您需要启动自定义选项卡,否则它不会改变任何内容

  • 使用
    文本视图创建一个新布局(您可以在每个选项卡中添加任何您想要的内容)
  • 在您的
    on活动中,在
    表格布局之后创建
    。设置WithViewPager
    启动自定义选项卡:

    for (int i = 0; i < 3; i++) { // 3 - A+B+C in your example
        TabLayout.Tab tab = tabLayout.getTabAt(i);
        if (tab != null) {
            ViewGroup tabContainer = (ViewGroup) LayoutInflater.from(this).inflate(R.layout.custom_tab_item, tabLayout, false);
            if (tabContainer != null) {
                TextView yourTv = (TextView) tabContainer.findViewById(R.id.tv); 
                yourTv.setTextSize(18);
                tab.setCustomView(tabContainer);
            }
        }
    }
    

  • 您可以将自己的视图设置为表格布局的各个选项卡,也可以在选项卡选择中更改后者的大小-

    以下是代码提示-

            TabLayout mTabLayout = (TabLayout) findViewById(R.id.tab_layout);
    
            TabLayout.Tab tabOne = mTabLayout.newTab();
            tabOne.setCustomView(getLayoutInflater().inflate(R.layout.item_tab, mTabLayout, false));
            mTabLayout.addTab(tabOne);
    
            TabLayout.Tab tabTwo = mTabLayout.newTab();
            tabTwo.setCustomView(getLayoutInflater().inflate(R.layout.item_tab, mTabLayout, false));
            mTabLayout.addTab(tabTwo);
            tabTwo.select();
            // mTabLayout.setupWithViewPager(mViewPager);
            if (getResources().getDisplayMetrics().widthPixels > getResources().getDisplayMetrics().heightPixels) {
                mTabLayout.setTabMode(TabLayout.MODE_FIXED);
            } else {
                mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
            }
    
    
            mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    ((TextView) tab.getCustomView().findViewById(R.id.text1)).setTextSize(16);
                }
    
                @Override
                public void onTabUnselected(TabLayout.Tab tab) {
                    ((TextView) tab.getCustomView().findViewById(R.id.text1)).setTextSize(13);
                }
    
                @Override
                public void onTabReselected(TabLayout.Tab tab) {
    
                }
            });
    
    iteb_tab.xml可以是-

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <TextView
            android:text="One"
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
    
    编辑

    通过从适配器本身设置选项卡标题,您可以进一步减少工作量-

            PagerAdapter mPagerAdapter = mViewPager.getAdapter();
            for (int position = 0; position < mPagerAdapter.getCount(); position++) {
                View view = (getLayoutInflater().inflate(R.layout.item_tab, mTabLayout, false));
                TextView label = (TextView) view.findViewById(R.id.text1);
                label.setText(mPagerAdapter.getPageTitle(position));
                TabLayout.Tab tab = mTabLayout.newTab();
                tab.setCustomView(view);
                mTabLayout.addTab(tab);
            }
    
    PagerAdapter mPagerAdapter=mviewpage.getAdapter();
    对于(int position=0;position
    下面是它的样子-

    谢谢你的回答,但是文本大小没有改变。我尝试过改变textColor-效果很好。知道为什么textSize没有改变吗?再次检查答案@lounchyIt Works!textSize正在改变,非常感谢!@Amir\u p,嗨,我正在使用你的答案,只是没有自定义视图,因为默认情况下TextView在TabView中存在,大小不会随着时间的推移而改变选择一个标签,但是如果setText方法确实改变了文本。TabLayout高度设置为wrap_内容,所以我找不到文本没有变大的任何原因。也许你可以帮我解决这个问题,并且如果你能解释TabLayout.tab和TabLayout.TabView之间的区别,因为我很难理解你为什么不直接修改您可以使用tab对象,而不是tab的位置?感谢您不用从view pager设置选项卡布局,而是使用您的代码进行设置。例如创建选项卡并为其设置自定义视图。
    for (int i = 0; i < tabLayout.getTabCount(); i++) {
        TabLayout.Tab tab = tabLayout.getTabAt(i);
        if (tab != null) {
            View customView = tab.getCustomView();
            if (customView != null) {
                TextView yourTv = (TextView) customView.findViewById(R.id.tv);
                if (yourTv != null) {
                    if (i == selectedTabIndex) {
                        yourTv.setTextSize(18);
                    } else {
                        yourTv.setTextSize(16);
                    }
                }
            }
        }
    }
    
            TabLayout mTabLayout = (TabLayout) findViewById(R.id.tab_layout);
    
            TabLayout.Tab tabOne = mTabLayout.newTab();
            tabOne.setCustomView(getLayoutInflater().inflate(R.layout.item_tab, mTabLayout, false));
            mTabLayout.addTab(tabOne);
    
            TabLayout.Tab tabTwo = mTabLayout.newTab();
            tabTwo.setCustomView(getLayoutInflater().inflate(R.layout.item_tab, mTabLayout, false));
            mTabLayout.addTab(tabTwo);
            tabTwo.select();
            // mTabLayout.setupWithViewPager(mViewPager);
            if (getResources().getDisplayMetrics().widthPixels > getResources().getDisplayMetrics().heightPixels) {
                mTabLayout.setTabMode(TabLayout.MODE_FIXED);
            } else {
                mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
            }
    
    
            mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    ((TextView) tab.getCustomView().findViewById(R.id.text1)).setTextSize(16);
                }
    
                @Override
                public void onTabUnselected(TabLayout.Tab tab) {
                    ((TextView) tab.getCustomView().findViewById(R.id.text1)).setTextSize(13);
                }
    
                @Override
                public void onTabReselected(TabLayout.Tab tab) {
    
                }
            });
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <TextView
            android:text="One"
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
    
                mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    
                }
                @Override
                public void onPageSelected(int position) {
                    mTabLayout.getTabAt(position).select();
                }
                @Override
                public void onPageScrollStateChanged(int state) {
                }
            });
    
            PagerAdapter mPagerAdapter = mViewPager.getAdapter();
            for (int position = 0; position < mPagerAdapter.getCount(); position++) {
                View view = (getLayoutInflater().inflate(R.layout.item_tab, mTabLayout, false));
                TextView label = (TextView) view.findViewById(R.id.text1);
                label.setText(mPagerAdapter.getPageTitle(position));
                TabLayout.Tab tab = mTabLayout.newTab();
                tab.setCustomView(view);
                mTabLayout.addTab(tab);
            }