【问题标题】:Updating Android Tab Icons更新 Android 标签图标
【发布时间】:2008-08-31 14:36:11
【问题描述】:

我有一个活动,它有一个 TabHost,其中包含一组 TabSpec,每个 TabSpec 都有一个包含要由选项卡显示的项目的列表视图。在创建每个 TabSpec 时,我设置了一个要在选项卡标题中显示的图标。

TabSpecs 是在 setupTabs() 方法中以这种方式创建的,该方法循环创建适当数量的选项卡:

TabSpec ts = mTabs.newTabSpec("tab");
ts.setIndicator("TabTitle", iconResource);

ts.setContent(new TabHost.TabContentFactory(
{
    public View createTabContent(String tag)
    {
        ... 
    }            
});
mTabs.addTab(ts);

在某些情况下,我希望能够在程序执行期间更改每个选项卡中显示的图标。目前,我正在删除所有选项卡,并再次调用上述代码重新创建它们。

mTabs.getTabWidget().removeAllViews();
mTabs.clearAllTabs(true);
setupTabs();

有没有办法在不删除和重新创建所有选项卡的情况下替换正在显示的图标?

【问题讨论】:

    标签: java android android-tabhost


    【解决方案1】:

    简短的回答是,您不会错过任何东西。 Android SDK 不提供在创建TabHost 后更改其指示符的直接方法。 TabSpec 仅用于构建选项卡,因此事后更改TabSpec 将没有效果。

    不过,我认为有一个解决方法。调用mTabs.getTabWidget() 获取TabWidget 对象。这只是ViewGroup 的子类,因此您可以调用getChildCount()getChildAt() 来访问TabWidget 中的各个选项卡。这些选项卡中的每一个也是一个视图,对于带有图形指示器和文本标签的选项卡,几乎可以肯定是其他一些ViewGroup(可能是LinearLayout,但没关系)包含一个ImageViewTextView。因此,稍微摆弄一下调试器或Log.i,您应该能够找到获取ImageView 并直接更改它的方法。

    不利的一面是,如果您不小心,选项卡中控件的确切布局可能会发生变化,您的应用可能会崩溃。您最初的解决方案可能更强大,但它可能会导致其他不需要的副作用,例如闪烁或焦点问题。

    【讨论】:

      【解决方案2】:

      只是为了确认 dominics 的答案,这是他的代码解决方案(实际上有效):

      tabHost.setOnTabChangedListener(new OnTabChangeListener() {
          public void onTabChanged(String tabId) {
              if (TAB_MAP.equals(tabId)) {
                  ImageView iv = (ImageView) tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.icon);
                  iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_map_black));
                  iv = (ImageView) tabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.icon);
                  iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_list_white));
              } else if (TAB_LIST.equals(tabId)) {
                  ImageView iv = (ImageView) tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.icon);
                  iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_map_white));
                  iv = (ImageView) tabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.icon);
                  iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_list_black));
              }
          }
      });
      

      当然,它根本没有经过优化,在 getChildAt() 中使用这些直接索引一点也不好......

      【讨论】:

      • 小改进 - 使用:TabWidget.getChildTabViewAt(..) 而不是 getChildAt(...)
      【解决方案3】:

      请参阅我的帖子,其中包含有关 Customized Android Tabs 的代码示例。

      谢谢 光谱

      【讨论】:

      • 太棒了!正是我想要的。谢谢!
      【解决方案4】:

      这就是我所做的,它对我有用。我在从 TabBarActivity 扩展的活动中创建了这个函数

      public void updateTab(int stringID) {
          ViewGroup identifyView = (ViewGroup)getTabWidget().getChildAt(0);
          TextView v =  (TextView)identifyView.getChildAt(identifyView.getChildCount() - 1);
          v.setText(stringID);
      }
      

      您可以修改此功能以更改图像而不是文本,也可以同时更改两者,也可以修改此功能以获取任何选项卡子项。我对在运行时修改第一个选项卡的文本特别感兴趣。

      我使用这个调用从相关活动中调用了这个函数

      getParent().updateTab(R.string.tab_bar_analyze);
      

      【讨论】:

      • 仅供参考:如果您使用的是 Material Tab Layout,请将方法第一行更新为 ViewGroup identifyView = (ViewGroup) tabLayout.getTabAt(0).getCustomView();快乐编码:)
      【解决方案5】:

      试试这个:

      tabHost.setOnTabChangedListener(new OnTabChangeListener() {
          public void onTabChanged(String tabId) {
              if (TAB_MAP.equals(tabId)) {
                  ImageView iv = (ImageView) tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.icon);
                  iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_map_black));
                  iv = (ImageView) tabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.icon);
                  iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_list_white));
              } else if (TAB_LIST.equals(tabId)) {
                  ImageView iv = (ImageView) tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.icon);
                  iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_map_white));
                  iv = (ImageView) tabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.icon);
                  iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_list_black));
              }
          }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-30
        • 1970-01-01
        • 1970-01-01
        • 2016-11-25
        • 1970-01-01
        相关资源
        最近更新 更多