如何动态更改android标签文本?

问题描述 投票:15回答:7

这看起来应该很简单,但我无法想出办法。我需要一个选项卡来开始文本,但是在用户从列表中选择一个项目后,该文本会发生变化。我知道如何通过改变标签背景和颜色

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

这也不起作用,还有其他一些尝试。有任何想法吗?提前谢谢!

android tabs android-widget
7个回答
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。你肯定让我指出了正确的方向。


6
投票

尝试这种最适合我的方式:

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

5
投票

你可以通过使用TextView而不知道findViewById的魔法指数来做到这一点:

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

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(...);

1
投票

这是你的布局:

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

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是系统生成的,只需根据需要更改ChildIndex和Text


0
投票

这很简单。试试吧。有用。

TabWidget tab = (TabWidget) findViewById (R.id.tab1);
((TextView)tab.getChildTabViewAt (1)).setText ("New name here");
© www.soinside.com 2019 - 2024. All rights reserved.