【问题标题】:How to change android tab text on the fly?如何动态更改android标签文本?
【发布时间】:2011-04-09 17:30:39
【问题描述】:

这看起来应该很简单,但我想不出办法。我需要一个选项卡来显示开始文本,但在用户从列表中选择一个项目后,该文本会发生变化。我知道如何通过

更改标签背景和颜色
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


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      哇。好吧,这很痛苦。显然 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),如this answer
      【解决方案3】:

      你可以在不知道TextView的魔法索引的情况下使用findViewById来做到这一点:

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

      【讨论】:

        【解决方案4】:

        这是你的布局:

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

        【讨论】:

          【解决方案5】:
          TabWidget vTabs = .....;
          
          // get desired tab view  
          View vTab = vTabs.getChildAt(i);
          
          // I guess vTab is instance of TextView
          TextView vText = (TextView) vTab;
          
          vText.setText(...);
          

          【讨论】:

          • 昨晚我可以玩一点,但它仍然在我身上爆炸。你有这个工作吗?今天我将对其进行更多的试验,但如果你有更多的洞察力,那就太好了。再次感谢。
          【解决方案6】:

          你可以这样做

          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根据你的需要修改

          【讨论】:

            【解决方案7】:

            这很简单。试试吧。它有效。

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2012-10-10
              • 1970-01-01
              • 1970-01-01
              • 2017-03-14
              • 2015-09-10
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多