【问题标题】:Change the font of tab text in android design support TabLayout在android设计支持TabLayout中更改标签文本的字体
【发布时间】:2015-09-13 01:24:19
【问题描述】:

我正在尝试使用 android 设计库中的新 TabLayout

我想将标签文本更改为自定义字体。而且,我尝试搜索一些与TabLayout 相关的样式,但最终找到了this

请指导如何更改标签文本字体。

【问题讨论】:

  • 使用可跨行字符串

标签: android fonts android-design-library android-fonts android-tablayout


【解决方案1】:

如果您使用TabLayout 并且想要更改字体,则必须在之前的解决方案中添加一个新的 for 循环,如下所示:

private void changeTabsFont() {
    ViewGroup vg = (ViewGroup) tabLayout.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);
                if (tabViewChild instanceof TextView) {
                    ((TextView) tabViewChild).setTypeface(Font.getInstance().getTypeFace(), Typeface.NORMAL);
                }
        }
    }
} 

请参考change font style in action bar tabs using sherlock

【讨论】:

  • 我没有使用 M 预览版。有没有其他方法可以做到。
  • 你不需要M预览,对所有使用TabLayout的人都有效
  • 但是,我在 android.view.View android.support.design.widget.TabLayout.getChildAt(int) 上遇到 NullPointerException,您能帮我解决一下吗?找不到我在代码中遗漏的内容。
  • setTypeFace 的第一个参数是TypeFace,以防万一你找不到Font 类(对我来说似乎不存在)
【解决方案2】:

创建您自己的自定义样式并将父样式用作parent="@android:style/TextAppearance.Widget.TabWidget"

在你的标签布局中使用这种风格作为app:tabTextAppearance="@style/tab_text"

示例: 风格:

<style name="tab_text" parent="@android:style/TextAppearance.Widget.TabWidget">
    <item name="android:fontFamily">@font/poppins_regular</item>
</style>

示例:标签布局组件:

<android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:tabTextAppearance="@style/tab_text" />

【讨论】:

  • 这是正确的,我使用的是parent="TextAppearance.Design.Tab"
  • 这比第一个答案好得多,而且没有黑魔法(隐藏的 api),这可能会在未来破坏一些东西
  • 使用fontFamily时有效,使用fontPath时无效
  • 在使用TextAppearance.Widget.TabWidget 时,我经常遇到奇怪的异常。 @Javatar 的回答为我解决了这个问题。
  • 我正在使用com.google.android.material.tabs.TabLayout,所以我的样式的父级是Widget.MaterialComponents.TabLayout,除了相同的解决方案,效果很好,谢谢。
【解决方案3】:

praveen Sharma 的回答很好。只是一个小补充: 您可以简单地使用自己的CustomTabLayout,而不是在需要TabLayout 的任何地方使用changeTabsFont()

import android.content.Context;
import android.graphics.Typeface;
import android.support.design.widget.TabLayout;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class CustomTabLayout extends TabLayout {
    private Typeface mTypeface;

    public CustomTabLayout(Context context) {
        super(context);
        init();
    }

    public CustomTabLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Regular.ttf");
    }

    @Override
    public void addTab(Tab tab) {
        super.addTab(tab);

        ViewGroup mainView = (ViewGroup) getChildAt(0);
        ViewGroup tabView = (ViewGroup) mainView.getChildAt(tab.getPosition());

        int tabChildCount = tabView.getChildCount();
        for (int i = 0; i < tabChildCount; i++) {
            View tabViewChild = tabView.getChildAt(i);
            if (tabViewChild instanceof TextView) {
                ((TextView) tabViewChild).setTypeface(mTypeface, Typeface.NORMAL);
            }
        }
    }

}

还有一件事。 TabView 是一个LinearLayout,内部带有TextView(它也可以选择包含ImageView)。所以你可以让代码更简单:

@Override
public void addTab(Tab tab) {
    super.addTab(tab);

    ViewGroup mainView = (ViewGroup) getChildAt(0);
    ViewGroup tabView = (ViewGroup) mainView.getChildAt(tab.getPosition());
    View tabViewChild = tabView.getChildAt(1);
    ((TextView) tabViewChild).setTypeface(mTypeface, Typeface.NORMAL);
}

但我不推荐这种方式。如果TabLayout 的实现发生变化,这段代码可能无法正常运行,甚至崩溃。

自定义TabLayout 的另一种方法是向其添加自定义视图。这是伟大的example

【讨论】:

  • addTab 不会在您设置这样的选项卡时调用: mViewPager.setAdapter(new YourPagerAdapter(getChildFragmentManager())); mTabLayout.setupWithViewPager(mViewPager);
  • @Penzzz 你是绝对正确的。在这种情况下,您应该将代码从 addTab 方法移动到另一个方法。例如 onMeasure。
  • @AndreiAulaska thnx 你拯救了我的一天。你的最后一个链接救了我。
  • 我认为在最新版本中这不再起作用。 @ejw 的答案现在有效。需要添加到 addTab(Tab tab, boolean setSelected)
  • 这很好用。注意:从支持库 25.x 开始,您需要覆盖 addTab(Tab tab, int position, boolean setSelected) 而不是 addTab(Tab tab)
【解决方案4】:

像这样从 Java 代码或 XML 创建一个 TextView

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:textSize="15sp"
    android:textColor="@color/tabs_default_color"
    android:gravity="center"
    android:layout_height="match_parent"
/>

请确保保持 id 原样,因为如果您使用自定义 textview,TabLayout 会检查此 ID

然后从代码中扩展此布局并在该文本视图上设置自定义 Typeface 并将此自定义视图添加到选项卡

for (int i = 0; i < tabLayout.getTabCount(); i++) {
     //noinspection ConstantConditions
     TextView tv = (TextView)LayoutInflater.from(this).inflate(R.layout.custom_tab,null)
     tv.setTypeface(Typeface);       
     tabLayout.getTabAt(i).setCustomView(tv);
}

【讨论】:

  • 在这种情况下如何设置tabTextColortabSelectedTextColor 属性?
  • 我从praveen Sharma's answer得到解决方案
【解决方案5】:

要在运行 Android 4.1(API 级别 16)及更高版本的设备上使用 XML 功能中的字体支持,请使用支持库 26+。

  1. 右键res文件夹
  2. 新建->Android资源目录->选择字体->确定
  3. 将您的myfont.ttf 文件放入新创建的字体文件夹中

res/values/styles.xml 添加:

<style name="customfontstyle" parent="@android:style/TextAppearance.Small">
    <item name="android:fontFamily">@font/myfont</item>
</style>

在布局文件中添加 app:tabTextAppearance="@style/customfontstyle",

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

请参考[xml中的字体]。(https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml)

【讨论】:

  • 它适用于 xamarin 表单和 xamarin android.thanks
  • 很好的答案!不过应该扩展“TextAppearance.Design.Tab”
【解决方案6】:

如果你正在使用

com.google.android.material:material:1.2.0(最新版本)

<style name="MyCustomTabTextAppearance" parent="TextAppearance.Design.Tab">
        <item name="fontFamily">Your Font</item>
        <item name="android:fontFamily">Your Font</item>
        <item name="textAllCaps">false</item>
    </style>

<com.google.android.material.tabs.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabBackground="@color/colorPrimary"
        app:tabGravity="fill"
        app:tabIndicatorColor="@color/white"
        app:tabMode="fixed"
        app:tabTextAppearance="@style/MyCustomTabTextAppearance"
        app:tabTextColor="@android:color/white" />

【讨论】:

    【解决方案7】:

    以下方法将递归更改整个ViewGroup 的字体。我选择这种方法是因为你不必关心TabLayout 的内部结构。我正在使用Calligraphy 库来设置字体。

    void changeFontInViewGroup(ViewGroup viewGroup, String fontPath) {
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            View child = viewGroup.getChildAt(i);
            if (TextView.class.isAssignableFrom(child.getClass())) {
                CalligraphyUtils.applyFontToTextView(child.getContext(), (TextView) child, fontPath);
            } else if (ViewGroup.class.isAssignableFrom(child.getClass())) {
                changeFontInViewGroup((ViewGroup) viewGroup.getChildAt(i), fontPath);
            }
        }
    }
    

    【讨论】:

    • 这个问题是,如果您将布局附加到 Viewpager,您将丢失自定义字体。
    【解决方案8】:

    正如 Andrei 回答的那样,您可以通过扩展 TabLayout 类来更改字体。正如 Penzzz 所说,你不能在 addTab 方法中做到这一点。重写 onLayout 方法如下:

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom){
        super.onLayout(changed, left, top, right, bottom);
    
        final ViewGroup tabStrip = (ViewGroup)getChildAt(0);
        final int tabCount = tabStrip.getChildCount();
        ViewGroup tabView;
        int tabChildCount;
        View tabViewChild;
    
        for(int i=0; i<tabCount; i++){
            tabView = (ViewGroup)tabStrip.getChildAt(i);
            tabChildCount = tabView.getChildCount();
            for(int j=0; j<tabChildCount; j++){
                tabViewChild = tabView.getChildAt(j);
                if(tabViewChild instanceof AppCompatTextView){
                    if(fontFace == null){
                        fontFace = Typeface.createFromAsset(context.getAssets(), context.getString(R.string.IranSans));
                    }
                    ((TextView) tabViewChild).setTypeface(fontFace, Typeface.BOLD);
                }
            }
        }
    }
    

    必须覆盖 onLayout 方法,因为当您使用 setupWithViewPager 方法将 TabLayout 与 ViewPager 绑定时,您必须在之后使用 setText 方法或在 PagerAdapter 中设置选项卡文本以及发生这种情况时, onLayout 方法在父 ViewGroup (TabLayout) 上被调用,这就是设置字体的地方。(更改 TextView 文本会导致调用其父级的 onLayout 方法 - tabView 有两个子级,一个是 ImageView 另一个是 TextView)

    另一种解决方案:

    首先,这几行代码:

        if(fontFace == null){
            fontFace = Typeface.createFromAsset(context.getAssets(), context.getString(R.string.IranSans));
        }
    

    在上面的解决方案中,应该写在两个循环之外。

    API >= 16 的更好解决方案是使用 android:fontFamily

    创建一个 Android 资源目录 命名字体并将所需的字体复制到该目录。

    然后使用这些样式:

    <style name="tabLayoutTitles">
        <item name="android:textColor">@color/white</item>
        <item name="android:textSize">@dimen/appFirstFontSize</item>
        <item name="android:fontFamily">@font/vazir_bold</item>
    </style>
    
    <style name="defaultTabLayout">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">@dimen/defaultTabLayoutHeight</item>
        <item name="android:gravity">right</item>
        <item name="tabTextAppearance">@style/tabLayoutTitles</item>
        <item name="tabSelectedTextColor">@color/white</item>
        <item name="tabIndicatorColor">@color/white</item>
        <item name="tabIndicatorHeight">@dimen/accomTabIndicatorHeight</item>
        <item name="tabMode">fixed</item>
        <item name="tabGravity">fill</item>
        <item name="tabBackground">@drawable/rectangle_white_ripple</item>
        <item name="android:background">@color/colorPrimary</item>
    </style>
    

    【讨论】:

    • 这是一个糟糕的性能修复,onLayout() 会在每次布局更改时被调用,例如标签切换甚至标签下方的列表滚动,在许多标签中嵌套 fors TabLayout 应用程序将懒惰。
    • @Amr Barakat。根据此链接:developer.android.com/reference/android/view/…, int, int, int, int),这是不正确的。我也测试过。我在 onLayout 方法中设置了一个断点,当创建标签而不是在标签切换或列表滚动时调用它。
    • 对我来说,onLayout() 在切换选项卡时确实会被多次调用(不知道究竟是为什么),但考虑到这一点,我只在boolean changed 为真时设置字体。这样做可以防止多次设置字体。
    • 很好的解决方案,不需要代码,只有xml属性
    【解决方案9】:

    对于设计支持 23.2.0,使用 setupWithViewPager,您必须将代码从 addTab(Tab tab) 移动到 addTab(Tab tab, boolean setSelected)。

    【讨论】:

      【解决方案10】:

      你可以用这个,它对我有用。

       private void changeTabsFont() {
          ViewGroup vg = (ViewGroup) tabLayout.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);
                  if (tabViewChild instanceof TextView) {
                      AssetManager mgr = getActivity().getAssets();
                      Typeface tf = Typeface.createFromAsset(mgr, "fonts/Roboto-Regular.ttf");//Font file in /assets
                      ((TextView) tabViewChild).setTypeface(tf);
                  }
              }
          }
      }
      

      【讨论】:

        【解决方案11】:

        嗯,我发现它在 23.4.0 中很简单,没有使用循环。只需按照@ejw 的建议覆盖 addTab(@NonNull Tab tab, boolean setSelected)。

        @Override
        public void addTab(@NonNull Tab tab, boolean setSelected) {
            CoralBoldTextView coralTabView = (CoralBoldTextView) View.inflate(getContext(), R.layout.coral_tab_layout_view, null);
            coralTabView.setText(tab.getText());
            tab.setCustomView(coralTabView);
        
            super.addTab(tab, setSelected);
        }
        

        这里是 XML

        <?xml version="1.0" encoding="utf-8"?>
        <id.co.coralshop.skyfish.ui.CoralBoldTextView
           xmlns:android="http://schemas.android.com/apk/res/android"
           android:id="@+id/custom_text"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:ellipsize="end"
           android:gravity="center"
           android:singleLine="true"
           android:textColor="@color/graylove"
           android:textSize="@dimen/tab_text_size" />
        

        希望它可以帮助:)

        【讨论】:

        • 但是如何设置tabSelectedTextColor和tabTextColo呢?
        • @MostafaImran 您的android:textColor="@color/graylove" 版本应该具有指定状态选择颜色的状态列表选择器
        【解决方案12】:

        对我有用的 Kotlin 扩展:

        fun TabLayout.setFont(font: FontUtils.Fonts) {
            val vg = this.getChildAt(0) as ViewGroup
            for (i: Int in 0..vg.childCount) {
                val vgTab = vg.getChildAt(i) as ViewGroup?
                vgTab?.let {
                    for (j: Int in 0..vgTab.childCount) {
                        val tab = vgTab.getChildAt(j)
                        if (tab is TextView) {
                            tab.typeface = FontUtils.getTypeFaceByFont(FontUtils.Fonts.BOLD, context)
                        }
                    }
                }
            }
        }
        

        【讨论】:

          【解决方案13】:

          我的Resolve方法就是这样,改变Specified tab text,

           ViewGroup vg = (ViewGroup) tabLayout.getChildAt(0);
           ViewGroup vgTab = (ViewGroup) vg.getChildAt(1);
           View tabViewChild = vgTab.getChildAt(1);
           if (tabViewChild instanceof TextView) {
                ((TextView) tabViewChild).setText(str);
           }
          

          【讨论】:

            【解决方案14】:
            I think this is easier way.
            
            <android.support.design.widget.TabLayout
               android:id="@+id/tabs"
               app:tabTextColor="@color/lightPrimary"
               app:tabSelectedTextColor="@color/white"
               style="@style/CustomTabLayout"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"/>
            <style name="CustomTabLayout" parent="Widget.Design.TabLayout">
               <item name="tabMaxWidth">20dp</item>
               <item name="tabMode">scrollable</item>
               <item name="tabIndicatorColor">?attr/colorAccent</item>
               <item name="tabIndicatorHeight">2dp</item>
               <item name="tabPaddingStart">12dp</item>
               <item name="tabPaddingEnd">12dp</item>
               <item name="tabBackground">?attr/selectableItemBackground</item>
               <item name="tabTextAppearance">@style/CustomTabTextAppearance</item>
               <item name="tabSelectedTextColor">?android:textColorPrimary</item>
            </style>
            <style name="CustomTabTextAppearance" parent="TextAppearance.Design.Tab">
               <item name="android:textSize">16sp</item>
               <item name="android:textStyle">bold</item>
               <item name="android:textColor">?android:textColorSecondary</item>
               <item name="textAllCaps">false</item>
            </style>
            

            【讨论】:

              【解决方案15】:

              这是我在 Kotlin 中的实现,它还允许更改选定和未选定选项卡的字体。

              class FontTabLayout @JvmOverloads constructor(
                  context: Context,
                  attrs: AttributeSet? = null,
                  @AttrRes defStyleAttr: Int = 0
              ) : TabLayout(context, attrs, defStyleAttr) {
              
                  private var textSize = 14f
              
                  private var defaultSelectedPosition = 0
              
                  private var selectedTypeFace: Typeface? = ResourcesCompat.getFont(context, R.font.muli_bold)
                  private var normalTypeFace: Typeface? = ResourcesCompat.getFont(context, R.font.muli_regular)
              
                  @ColorInt private var selectedColor = 0
                  @ColorInt private var normalTextColor = 0
              
                  init {
                      attrs?.let { initAttrs(it) }
                      addOnTabSelectedListener()
                  }
              
                  private fun initAttrs(attrs: AttributeSet) {
                      val a = context.obtainStyledAttributes(attrs, R.styleable.FontTabLayout)
              
                      textSize = a.getDimensionPixelSize(R.styleable.FontTabLayout_textSize, 14).toFloat()
              
                      defaultSelectedPosition = a.getInteger(R.styleable.FontTabLayout_defaultSelectedPosition, 0)
                      val selectedResourceId = a.getResourceId(R.styleable.FontTabLayout_selectedTypeFace, R.font.muli_bold)
                      val normalResourceId = a.getResourceId(R.styleable.FontTabLayout_normalTypeFace, R.font.muli_regular)
              
                      selectedColor = a.getColor(com.google.android.material.R.styleable.TabLayout_tabSelectedTextColor, 0)
                      normalTextColor = a.getColor(R.styleable.FontTabLayout_normalTextColor, 0)
              
                      selectedTypeFace = ResourcesCompat.getFont(context, selectedResourceId)
                      normalTypeFace = ResourcesCompat.getFont(context, normalResourceId)
              
                      a.recycle()
                  }
              
                  private fun addOnTabSelectedListener() {
                      addOnTabSelectedListener(object : OnTabSelectedListenerAdapter() {
              
                          override fun onTabUnselected(tab: Tab?) {
                              getCustomViewFromTab(tab)?.apply {
                                  setTextColor(normalTextColor)
                                  typeface = normalTypeFace
                              }
                          }
              
                          override fun onTabSelected(tab: Tab?) {
              
                              getCustomViewFromTab(tab)?.apply {
                                  setTextColor(selectedColor)
                                  typeface = selectedTypeFace
                              }
                          }
              
                          private fun getCustomViewFromTab(tab: Tab?) = tab?.customView as? AppCompatTextView
              
                      })
                  }
              
                  override fun setupWithViewPager(viewPager: ViewPager?, autoRefresh: Boolean) {
                      super.setupWithViewPager(viewPager, autoRefresh)
                      addViews(viewPager)
                  }
              
                  private fun addViews(viewPager: ViewPager?) {
                      for (i in 0 until tabCount) {
                          val customTabView = getCustomTabView(i).apply {
                              typeface = if (i == defaultSelectedPosition) selectedTypeFace else normalTypeFace
                              val color = if (i == defaultSelectedPosition) selectedColor else normalTextColor
                              setTextColor(color)
                              text = viewPager?.adapter?.getPageTitle(i)
                          }
              
                          getTabAt(i)?.customView = customTabView
                      }
                  }
              
                  private fun getCustomTabView(position: Int): AppCompatTextView {
                      return AppCompatTextView(context).apply {
                          gravity = Gravity.CENTER
                          textSize = this@FontTabLayout.textSize
                          text = position.toString()
                      }
                  }
              }
              

              在 attrs.xml 中:

              <declare-styleable name="FontTabLayout">
                  <attr name="normalTextColor" format="reference|color" />
                  <attr name="textSize" format="dimension" />
                  <attr name="defaultSelectedPosition" format="integer" />
                  <attr name="selectedTypeFace" format="reference" />
                  <attr name="normalTypeFace" format="reference" />
              </declare-styleable>
              

              【讨论】:

              • tabs.getTabAt(1)?.text 不会动态更改文本,一旦设置。
              【解决方案16】:

              我的 2p,带有引用检​​查的 Kotlin,适用于任何地方,因为如果出现问题,它将停止。

              private fun setTabLayouFont(tabLayout: TabLayout) {
                  val viewGroupTabLayout = tabLayout.getChildAt(0) as? ViewGroup?
                  (0 until (viewGroupTabLayout?.childCount ?: return))
                          .map { viewGroupTabLayout.getChildAt(it) as? ViewGroup? }
                          .forEach { viewGroupTabItem ->
                              (0 until (viewGroupTabItem?.childCount ?: return))
                                      .mapNotNull { viewGroupTabItem.getChildAt(it) as? TextView }
                                      .forEach { applyDefaultFontToTextView(it) }
                          }
              }
              

              【讨论】:

                【解决方案17】:

                您可以使用 style.xml 文件中编写的此样式更改选项卡图标的文本外观。在这里

                <style name="TabItemTextAppearance" parent="TextAppearance.Design.Tab">
                        <item name="android:textSize">13sp</item>
                        <item name="fontWeight">800</item>
                        <item name="fontFamily">@font/balooda2_medium</item>
                </style>
                

                并在您的 TabLayout 中使用此样式。在这里

                <com.google.android.material.tabs.TabLayout
                                android:id="@+id/live_tab_bar"
                                android:layout_width="match_parent"
                                android:layout_height="@dimen/_40sdp"
                                app:tabGravity="fill"
                                app:tabIndicatorGravity="stretch"
                                app:tabMaxWidth="0dp"
                                app:tabMode="fixed"
                                app:tabSelectedTextColor="@color/white"
                                app:tabTextAppearance="@style/TabItemTextAppearance"
                                app:tabTextColor="#354895">
                
                                <com.google.android.material.tabs.TabItem
                                    android:id="@+id/live_tab"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:text="লাইভ ক্লাস" />
                
                                <com.google.android.material.tabs.TabItem
                                    android:id="@+id/recorded_tab"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:text="রেকর্ডেড ভিডিও" />
                
                </com.google.android.material.tabs.TabLayout>
                

                【讨论】:

                  【解决方案18】:

                  使用 kotlin 扩展函数使用这个:

                   fun TabLayout.setFontSizeAndColor(typeface: Typeface, @DimenRes textSize: Int, @ColorRes textColor: Int) {
                  val viewGroup: ViewGroup = this.getChildAt(0) as ViewGroup
                  val tabsCount: Int = viewGroup.childCount
                  for (j in 0 until tabsCount) {
                      val viewGroupTab: ViewGroup = viewGroup.getChildAt(j) as ViewGroup
                      val tabChildCount: Int = viewGroupTab.childCount
                      for (i in 0 until tabChildCount) {
                          val tabViewChild: View = viewGroupTab.getChildAt(i) as View
                          if ( tabViewChild is TextView) {
                              tabViewChild.typeface = typeface
                              tabViewChild.gravity = Gravity.FILL
                              tabViewChild.maxLines = 1
                              tabViewChild.setTextSize(TypedValue.COMPLEX_UNIT_PX, this.resources.getDimension(textSize))
                              tabViewChild.setTextColor(ContextCompat.getColor(this.context, textColor))
                          }
                      }
                  }
                  

                  }

                  【讨论】:

                    【解决方案19】:

                    改变

                    if (tabViewChild instanceof TextView) {

                    if (tabViewChild instanceof AppCompatTextView) { 
                    

                    使其与 android.support.design.widget.TabLayout 一起工作(至少来自 com.android.support:design:23.2.0)

                    【讨论】:

                    • 但是 AppCompatTextView 扩展了 TextView,为什么这会有所作为?
                    猜你喜欢
                    • 2015-08-24
                    • 2015-10-06
                    • 1970-01-01
                    • 2016-01-17
                    • 2023-03-04
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2015-08-17
                    相关资源
                    最近更新 更多