【问题标题】:tablayout change tab selected textsizetablayout 更改选项卡选定的文本大小
【发布时间】:2017-02-22 00:33:24
【问题描述】:

Tablayout.tab 无法更改代码中选定的文本大小

.

textAppearance 也不能设置 selectTextsize。那么如何完成呢?

【问题讨论】:

  • 您是否在xml中设置了TabLayout的样式?请在您的问题中包含 xml。
  • 当然我在xml中设置了TabLayout的样式,但是如果选择了我想改变标签文本大小,它只能设置一个固定文本大小。

标签: android android-support-library android-tablayout


【解决方案1】:

试试下面的。请注意实现选项卡选择的侦听器。

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

【讨论】:

  • TextView title = (TextView)(((LinearLayout) ((LinearLayout) mTab​​Layout.getChildAt(0)).getChildAt(tab.getPosition())).getChildAt(1));标题.setTextSize(50);
  • title.setTextSize(50),这个方法很奇怪,如果我设置TextSize(10),它可以工作,标签的文本大小很小,但是当我设置文本大小超过10时,无论标签是什么,它总是使用默认的文本大小选择与否。
  • 抱歉,我无法回答您的问题,因为我对此一无所知。也许您希望用我的回答打开一个新问题,以进一步澄清您的问题。
  • 无论如何,谢谢你的回答。我需要为它打开一个新问题
【解决方案2】:
 class OnTabSelectedListener implements TabLayout.OnTabSelectedListener{
        @Override
        public void onTabSelected(TabLayout.Tab selectedTab) {
            LinearLayout tabLayout1 = (LinearLayout)((ViewGroup) tabLayout.getChildAt(0)).getChildAt(selectedTab.getPosition());
            TextView tabTextView = (TextView) tabLayout1.getChildAt(1);
           // tabTextView.setTypeface(tabTextView.getTypeface(), Typeface.BOLD);
            tabTextView.setTextSize(20);
        }

        @Override
        public void onTabUnselected(TabLayout.Tab unselectedTab) {
            LinearLayout tabLayout1 = (LinearLayout)((ViewGroup) tabLayout.getChildAt(0)).getChildAt(unselectedTab.getPosition());
            TextView tabTextView = (TextView) tabLayout1.getChildAt(1);
            //tabTextView.setTypeface(tabTextView.getTypeface(), Typeface.NORMAL);
            tabTextView.setTextSize(15);
        }

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

        }
    }

用 textview 做任何事情都可以享受....

【讨论】:

    【解决方案3】:

    显示粗体字

    tabLayout.addOnTabSelectedListener(tabSelectedListener)
    
    private val tabSelectedListener = object : TabLayout.OnTabSelectedListener {
      override fun onTabSelected(tab: TabLayout.Tab?) {
        tab?.view?.children?.forEach {
          if (it is TextView) {
            it.post {
              it.setTypeface(ResourcesCompat.getFont(context, R.font.bold_font_name), Typeface.NORMAL)
            }
          }
        }
      }
    
      override fun onTabUnselected(tab: TabLayout.Tab?) {
        tab?.view?.children?.forEach {
          if (it is TextView) {
            it.post {
              it.setTypeface(ResourcesCompat.getFont(context, R.font.font_name), Typeface.NORMAL)
            }
          }
        }
      }
    
      override fun onTabReselected(tab: TabLayout.Tab?) {}
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-22
      • 1970-01-01
      • 2016-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-26
      相关资源
      最近更新 更多