如何在Android中动态更改选项卡文本?

20

这似乎应该很简单,但我无法找到方法。我需要一个选项卡有开头的文本,但是当用户从列表中选择项目后,该文本会更改。我知道如何通过

来更改选项卡的背景和颜色。
mTabHost.getChildAt(index).setBackgroundColor();

但是没有选项可以更改选项卡的指示器。我尝试使用EditText。

private EditText tabName;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.statistics);

    comm = new Communicator();

    tabName = new EditText(this);
    tabName.setText("BeginningText");

    mTabHost = getTabHost();
    mTabHost.addTab(mTabHost
                   .newTabSpec("tab_1_stat")
                   .setIndicator(User)
                   .setContent(R.id.meStatsTab));
    mTabHost.addTab(mTabHost
                   .newTabSpec("tab_2_stat")
                   .setIndicator(tabName.getText())
                   .setContent(R.id.themStatsTab));
    mTabHost.addTab(mTabHost
                   .newTabSpec("tab_3_stat")
                   .setIndicator("Archive")
                   .setContent(R.id.archiveStatsTab));
    mTabHost.setOnTabChangedListener(this);
    getTabWidget().getChildAt(1).setOnClickListener(new onThemTabClicked());
    mTabHost.setCurrentTab(0);

    onTabChanged("tab_1_stat");

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    tabName.setText("ChangedTabText");
    Bundle extras = intent.getExtras();
    themStats = extras.getStringArray("themStats");
    mTabHost.setCurrentTab(1);
    onTabChanged("tab_2_stat");
}

这种方法也不起作用,我还尝试了几种其他方法。有什么想法吗?提前感谢!

7个回答

19

试试这个最简单的方法,这对我有效:

tabLayout.getTabAt(index).setText("TabName");

8

哇,这真是一件痛苦的事情。显然TabWidget在RelativeLayout方面做了一些奇怪的事情,每次我尝试使用radek-k的解决方案时,都会出现RelativeLayout错误。因此,基本上解决方法如下。它还允许您更改字体、字体大小、字体颜色等。

TabWidget vTabs = getTabWidget();
RelativeLayout rLayout = (RelativeLayout) vTabs.getChildAt(tabIndex);
((TextView) rLayout.getChildAt(textIndex)).setText("NewTabText");

或者在一行中...
((TextView)((RelativeLayout)getTabWidget().getChildAt(tabIndex)).getChildAt(textIndex)).setText("NewTabText");

“textIndex”是文本字段的索引。在这种情况下,它是1。如果标签有图标或自定义修改,则索引可能会发生变化。

再次感谢radek-k。您确实让我朝着正确的方向前进了。


如果您正在使用标准的TabActivity,则可以使用findViewById(android.R.id.title)代替getChildAt(textIndex),就像这个答案中所示。 - Dheeraj Vepakomma

5

通过使用 findViewById,您可以在不知道 TextView 的魔术索引的情况下完成它:

TabWidget vTabs = getTabWidget();
View indicatorView = vTabs.getChildAt(tabIndex);
((TextView) indicatorView.findViewById(android.R.id.title)).setText("NewTabText");

3
这是你的布局:

<com.google.android.material.tabs.TabLayout
    android:id="@+id/ref_tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.google.android.material.tabs.TabItem
        android:id="@+id/ref_book"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <com.google.android.material.tabs.TabItem
        android:id="@+id/ref_chpter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <com.google.android.material.tabs.TabItem
        android:id="@+id/ref_verse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</com.google.android.material.tabs.TabLayout>

以下是您的代码:

TabLayout tabLayout = findViewById(R.id.ref_tabs);
tabLayout.getTabAt(0).setText("BOOK"));
tabLayout.getTabAt(1).setText("CHAPTER"));
tabLayout.getTabAt(2).setText("VERSE"));

2
TabWidget vTabs = .....;

// get desired tab view  
View vTab = vTabs.getChildAt(i);

// I guess vTab is instance of TextView
TextView vText = (TextView) vTab;

vText.setText(...);

我昨晚能够试着操作了一下,但它仍然会崩溃。你让它正常工作了吗?我今天还要继续尝试一些实验,但如果你有更深入的见解那就太好了。再次感谢。 - Honeal

0

这很简单易懂。试一下吧,它有效。

TabWidget tab = (TabWidget) findViewById (R.id.tab1);
((TextView)tab.getChildTabViewAt (1)).setText ("New name here");

0

你可以这样做

TabHost tabHost = getTabHost();
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.title);
tv.setText("New Tab Text");

android.R.id.title 是系统生成的,只需根据您的需要更改 ChildIndexText


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接