9

I want to programmatically change a tab's textview. Is there any way to do this?

There are answers only concerning the old TabHost view, I am using the TabLayout used by Google's Material Design library using android.support.design.widget.TabLayout.

For TabHost: How to add padding to a tabs label?

4

7 回答 7

17

This is working solution

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

Just change the last index Zero to One now above code will work

Removed Crash

java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.TextView
于 2016-02-16T13:02:59.847 回答
8

With a given tab:

 int wantedTabIndex = 0;
 TextView tv = (TextView)(((LinearLayout)((LinearLayout)mTabLayout.getChildAt(0)).getChildAt(wantedTabIndex)).getChildAt(0));
 tv.setText("Hello world!");
于 2015-07-08T00:15:29.947 回答
6
    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);
            }
        }
    }

Pass in your TabLayout to this recursive method. It will find any child that is a TextView and set its All Caps mode off. Avoids all the other very specific typecasting. If it does not appear to work call it later in your code. I had it in onCreate but that did not work. Called it later in code and it worked perfectly.

Affects all Tabs, not just one but I figure this is the most common usage. Not specific to TabLayout either. Can be used for any layout that contains TextViews you want to change.

于 2015-12-02T15:57:22.100 回答
1

This worked for me:

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

Replace 0 with the index of the tab you want to edit. I think your tabLayout needs to be populated before calling this, otherwise tablayout.getTabAt(0) will return null.

于 2017-07-31T13:38:38.890 回答
0

I think the best thing to do here is create a custom layout with a TextView and then assign the custom view to the tabs you want to edit

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

In this way you can do whatever you want with the TextView that is inside the layout

于 2016-02-03T00:11:34.477 回答
0

Add onTabSelectedListener to your tabLayout to set text dynamically. so in this approach when tab will be selected text will become "selected" or other tab

 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");
        }
    });

to set tab as default selected then use this statement

tabLayout.getTabAt(0).select(); //this is used to set tab as default
于 2016-11-07T10:55:31.557 回答
0

Kevin answer is great! (https://stackoverflow.com/a/34047188/7699617). Relying on this answer I create realization for apply any changes for TextView in Kotlin:

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)
        }
    }
}

How it use:

applyForTextView(tabLayout) { it.isAllCaps = false }
于 2021-05-25T06:41:11.807 回答