【问题标题】:Changing Text Size in Tabs of Tab Layout, when Pressed in android在 android 中按下时更改选项卡布局的选项卡中的文本大小
【发布时间】:2019-02-26 08:55:10
【问题描述】:

我想知道如何在TabLayout 的选定选项卡上将文本大小更改为更大。在其他 android 视图组件中,它可以使用选择器来实现,但我在 TabLayout 上尝试过选择器,它不起作用。我怎样才能做到这一点? 感谢您的关注。

【问题讨论】:

    标签: android tabs


    【解决方案1】:
                ********---BottomNavigation Style---********
    <style name="AppTheme.BottomNavigation"parent="Widget.MaterialComponents.BottomNavigationView">
        <item name="android:background">@color/colorWhite</item>
        <item name="labelVisibilityMode">labeled</item>
        <item name="itemIconTint">@color/bottom_nav_icon_color_selector</item>
        <item name="itemTextColor">@color/bottom_nav_icon_color_selector</item>
        <item name="itemTextAppearanceActive">@style/TextAppearanceActive</item>
        <item name="itemTextAppearanceInactive">@style/TextAppearanceInactive</item>
    </style>
    
    
    
                ********---Font Size Style---********
    <style name="TextAppearanceActive" parent="android:TextAppearance">
        <item name="android:fontFamily">@font/font_sf_regular</item>
        <item name="fontFamily">@font/font_sf_regular</item>
        <item name="fontStyle">normal</item>
        <item name="android:textSize">@dimen/font10</item>
    </style>
    
    
    //InActive Style
     <style name="TextAppearanceInactive" parent="TextAppearanceActive">
            <item name="android:textSize">@dimen/font9</item>
    </style>
    
    
    <com.google.android.material.bottomnavigation.BottomNavigationView
                android:id="@+id/navigation"
                style="@style/AppTheme.BottomNavigation"
                android:layout_width="match_parent"
                android:layout_height="@dimen/_50dp"
                android:layout_gravity="bottom"
                app:menu="@menu/navigation" />
    

    【讨论】:

      【解决方案2】:

      下面最简单的答案-

      tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
              @Override
              public void onTabSelected(TabLayout.Tab tab) {
                  TextView textView = (TextView) tab.getCustomView();
                  if (textView != null) {
                      textView.setTextColor(getResources().getColor(R.color.c_white));
                      textView.setTextSize(24);
                  }
              }
      
              @Override
              public void onTabUnselected(TabLayout.Tab tab) {
                  TextView textView = (TextView) tab.getCustomView();
                  if (textView != null) {
                      textView.setTextColor(getResources().getColor(R.color.c_grey));
                      textView.setTextSize(18);
                  }
              }
      
              @Override
              public void onTabReselected(TabLayout.Tab tab) {
              }
          });
      

      【讨论】:

      • 您的答案看起来很有希望,但 textView 为空。所以它让我无处可去
      • TextView textView = (TextView)(((LinearLayout)((LinearLayout)mTabLayout.getChildAt(0)).getChildAt(tab.getPosition())).getChildAt(1));
      猜你喜欢
      • 2018-05-13
      • 2013-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-22
      • 2017-02-22
      • 1970-01-01
      相关资源
      最近更新 更多