TabLayout更改选定的TabSize

时间:2016-10-19 02:24:06

标签: android

TextView title = (TextView)(((LinearLayout ((LinearLayout) mTabLayout.getChildAt(0)).getChildAt(tabPosition)).getChildAt(1));title.setTextSize(...);

我通过此方法获得Textview中的标签Tablayout,但这种方法很奇怪,如果我setTextSize(10),它有效,标签有小的文本大小,但是当{{1}在10以上,它总是使用默认的文本大小选择或不选择任何标签。我认为它只有两个默认的文本大小,任何人都遇到了这个问题,我怎么能为它设置其他文本化?

2 个答案:

答案 0 :(得分:0)

您可以尝试这个..您可以设置每个标签文字大小和其他属性..您可以在这里查看更多信息Custom tablayout

        ViewGroup vg = (ViewGroup) tabs.getChildAt(0);
        int tabsCount = vg.getChildCount();
        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);
                // Get TextView Element
                if (tabViewChild instanceof TextView) {
                  // change font
                  ((TextView) tabViewChild).setTypeface(tf);
                  // change color
                  ((TextView) tabViewChild).setTextColor(getResources().getColor(R.color.white));
                  // change size
                  ((TextView) tabViewChild).setTextSize(18);
                  // change padding
                  tabViewChild.setPadding(0, 0, 0, 0);
                  //..... etc...
                }
            }
        }

答案 1 :(得分:0)

private void tabLayoutInit() {
    LayoutInflater mLayoutInflater = this.getLayoutInflater();
    for (int i = 0; i < 5; i++) {
        TabLayout.Tab tab = mTabLayout.newTab();
        View view = mLayoutInflater.inflate(R.layout.tablayout_customview, null);
        tab.setCustomView(view);
        TextView text = (TextView) view.findViewById(R.id.tabLayout_customView_tv);
        text.setText(getResources().getStringArray(R.array.tab_resource)[i]);
        if (i==0) {
            text.setTextColor(getResources().getColor(R.color.color_f));
            text.setTextSize(DensityUtils.px2sp(MainActivity.this,50));
            mTabLayout.addTab(tab);
            continue;
        }
        text.setTextColor(getResources().getColor(R.color.fireBar));
        text.setTextSize(DensityUtils.px2sp(MainActivity.this,40));
        mTabLayout.addTab(tab);
    }
    final TabLayout.TabLayoutOnPageChangeListener listener =
            new TabLayout.TabLayoutOnPageChangeListener(mTabLayout);
    mViewPager.addOnPageChangeListener(listener);
    mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            TextView textView = (TextView) tab.getCustomView().findViewById(R.id.tabLayout_customView_tv);
            textView.setTextSize(DensityUtils.px2sp(MainActivity.this,50));
            textView.setTextColor(getResources().getColor(R.color.color_f));
            mViewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
            TextView textView = (TextView) tab.getCustomView().findViewById(R.id.tabLayout_customView_tv);
            textView.setTextColor(getResources().getColor(R.color.fireBar));
            textView.setTextSize(DensityUtils.px2sp(MainActivity.this,40));
        }

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

        }
    });
}