【问题标题】:Getting the textview of the TabLayout's tab获取 TabLayout 选项卡的 textview
【发布时间】:2015-07-08 00:15:29
【问题描述】:

我想以编程方式更改选项卡的文本视图。有没有办法做到这一点?

只有关于旧 TabHost 视图的答案,我正在使用 Google 的 Material Design 库使用的 TabLayout,使用 android.support.design.widget.TabLayout.

对于 TabHost: How to add padding to a tabs label?

【问题讨论】:

  • 漫画家的声音“最糟糕,问题,everrr”。开个玩笑,但是在这里阅读如何写一个好问题 - stackoverflow.com/help/how-to-ask。将来您应该包括代码、详细描述以及您已经尝试过的内容。
  • 在这种情况下提供代码相当困难,我尝试的是下面的答案,只是分享,因为得到这个答案特别棘手。然而详细的描述......

标签: android tabs material-design android-tablayout


【解决方案1】:

这是可行的解决方案

int wantedTabIndex = 0;
TextView tv = (TextView)(((LinearLayout)((LinearLayout)mTabLayout.getChildAt(0)).getChildAt(wantedTabIndex)).getChildAt(1));
tv.setText("Hello world!");

只需将最后一个索引 Zero 更改为 One,上面的代码就可以工作了

删除崩溃

java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.TextView

【讨论】:

  • 1 表示什么。例如我有 2 个标签。想要更新两者的文本样式。???
  • 每个选项卡都是一个线性布局。所以你的 LinearLayout 有 2 个选项卡/视图。在每个选项卡内,TextView 的索引为 1。
【解决方案2】:

使用给定的标签:

 int wantedTabIndex = 0;
 TextView tv = (TextView)(((LinearLayout)((LinearLayout)mTabLayout.getChildAt(0)).getChildAt(wantedTabIndex)).getChildAt(0));
 tv.setText("Hello world!");

【讨论】:

  • 这与支持库中的java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.TextView 崩溃22.2.1
  • 我也在通过“compile 'com.android.support:design:22.2.1'”运行 22.2.1,用一些代码告诉我......希望我能帮助你
  • 你能详细说明一下吗?得到同样的例外。代码已经看起来很糟糕。大声笑
  • 这不是一个安全的方法。 android 团队可以随时更新视图组层次结构,这会在更新支持库后导致应用崩溃
【解决方案3】:
    public static void setTextViewsCapsOff(View view) {
        if (!(view instanceof ViewGroup)) {
            return;
        }
        ViewGroup group = (ViewGroup)view;
        for (int i = 0; i < group.getChildCount(); i++) {
            View child = group.getChildAt(i);
            if (child instanceof TextView) {
                ((TextView)child).setAllCaps(false);
            } else {
                setTextViewsCapsOff(child);
            }
        }
    }

将您的 TabLayout 传递给此递归方法。它将找到任何作为 TextView 的子项并将其 All Caps 模式设置为关闭。避免所有其他非常具体的类型转换。如果它似乎不起作用,请稍后在您的代码中调用它。我在 onCreate 中有它,但这不起作用。后来在代码中调用它,它工作得很好。

影响所有选项卡,不仅仅是一个,但我认为这是最常见的用法。也不特定于 TabLayout。可用于包含要更改的 TextView 的任何布局。

【讨论】:

  • 这是一种比公认的解决方案更清洁、更面向未来的解决方案?
【解决方案4】:

这对我有用:

tabLayout.getTabAt(0).setText("some text");

0 替换为您要编辑的选项卡的索引。我认为你的 tabLayout 需要在调用它之前填充,否则 tablayout.getTabAt(0) 将返回 null

【讨论】:

    【解决方案5】:

    我认为最好的做法是使用 TextView 创建自定义布局,然后将自定义视图分配给您要编辑的选项卡

    final TabLayout.Tab tabAt = tabLayout.getTabAt(index);
    tabAt.setCustomView(myLayoutId);
    

    通过这种方式,你可以对布局内的 TextView 做任何你想做的事情

    【讨论】:

    • tabLayout.getTabAt(index); 返回 null 值,因为我的选项卡布局最初为空
    【解决方案6】:

    onTabSelectedListener 添加到您的 tabLayout 以动态设置文本。 因此,在此方法时,选中选项卡将被选中文本将变为“选择”或其他选项卡

     tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tab.getCustomView().setAlpha(1.0f);
                ((TextView) tab.getCustomView()).setTextSize(12);
                ((TextView) tab.getCustomView()).setText("Selected");
                Log.d("TabBar", "selected");
            }
    
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.getCustomView().setAlpha(0.3f);//to set alpha 
    
                ((TextView) tab.getCustomView()).setTextSize(11);
                ((TextView) tab.getCustomView()).setText("DeSelected");
                Log.d("TabBar", "unselected");
            }
    
            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                Log.d("TabBar", "reselected");
            }
        });
    

    将选项卡设置为默认选择然后使用此语句

    tabLayout.getTabAt(0).select(); //this is used to set tab as default
    

    【讨论】:

      【解决方案7】:

      凯文的回答很棒! (https://stackoverflow.com/a/34047188/7699617)。依靠这个答案,我创建了实现以在Kotlin 中应用TextView 的任何更改:

      private fun applyForTextView(parent: ViewGroup, func: (TextView) -> Unit) {
          for (i in 0 until parent.childCount) {
              when (val child = parent.getChildAt(i)) {
                  is TextView -> func(child)
                  is ViewGroup -> applyForTextView(child, func)
              }
          }
      }
      

      如何使用:

      applyForTextView(tabLayout) { it.isAllCaps = false }
      

      【讨论】:

        猜你喜欢
        • 2015-08-28
        • 1970-01-01
        • 2015-08-25
        • 2015-09-28
        • 2015-09-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多