【问题标题】:'Unselect' a tab in a TabHost在 TabHost 中“取消选择”选项卡
【发布时间】:2011-12-21 01:55:58
【问题描述】:

我有一个包含 5 个标签的 TabHost。 据我所知,必须始终选择一个选项卡。

我需要一种方法来取消选择我的所有选项卡,这样就不会选择任何选项卡。

如果 tabhost 的一般意思是始终选择一个选项卡, 我怎样才能让它看起来(UI 说话)好像标签没有被选中?

【问题讨论】:

    标签: android tabs android-tabhost unselect


    【解决方案1】:

    试试这个:

    final TabWidget tabWidget = tabHost.getTabWidget();
    final int n = tabWidget.getChildCount();
    for (int i = 0; i < n; ++i) {
        tabWidget.getChildAt(i).setSelected(false);
    }
    

    或者您可以添加隐藏选项卡并在需要取消选择选项卡时选择它

    tabHost.addTab(
                tabHost.newTabSpec("hiddenTab").setIndicator(""),
                MyFragment.class,
                null
        );
    
    tabHost.getTabWidget().getChildTabViewAt(hiddenTabIndex).setVisibility(View.GONE);
    

    并在需要时选择此选项卡

    tabHost.setCurrentTab(hiddenTabIndex);
    

    【讨论】:

      【解决方案2】:

      这是不可能的 AFAIK。但是,是的,您可以将选定选项卡的颜色设置为看起来未选中,并通过在将其设为“未选中”时管理全局变量并在希望将其正常显示给用户时设置正常布局来在其上设置空白布局.但这是一种诡计。

      希望你明白我的意思!

      编辑:

      假设您在代码中的某处设置了String what="disappear" 以显示它“未选中”,那么您可以使用此函数更改选项卡的颜色:

      Main.class:

      //Change The Backgournd Color of Tabs
          public void setTabColor(TabHost tabhost) {
      
      
              for(int i=0;i<tabhost.getTabWidget().getChildCount();i++)
              {
                      tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#FFFFFF"))); //unselected white colored                   
              }
      
                  if(!what.equals("disappear"))
                       tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("FF0000"))); // selected red colored
      
                  else
                       tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("FFFFFF"))); // selected but show as unselected with white color
      
      
          }
      

      在您的活动类中(由该选定选项卡打开):

      FirstActivity.class:

      if(what.equals("disappear"))
            setContentView(R.layout.blank);
      else
            setContentView(R.layout.first_layout);
      

      空白.xml:

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:id="@+id/layout"
        android:background="#ffffff"  
        android:gravity="center">
        <!-- You can make background transperent by setting it to "00ffffff" -->
        <!-- You can also add this textview to guide user -->
        <!--
            <TextView
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:text="Click Any Tab To Start
               />
        -->
      </LinearLayout>
      

      【讨论】:

      • 你能提供一些基本的后续步骤吗?就像我必须为此使用的系统颜色一样?究竟如何创建空白布局并将其设置为特定选项卡?
      • 首先,感谢您的帮助。现在,我已经设法使用您的代码更改选项卡的颜色,并且我不想使用 setBackgroundDrawable() 更改选项卡的背景。我正在尝试为未选择的选项卡获取原始可绘制对象。我发现资源的名称是:tab_unselected.9 但我不知道如何将它用作选项卡的backgroundDrawable。我试过 tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_unselected.9));但显然这是错误的。我该如何使用该资源?还是有更聪明的方法来获取原始可绘制对象?
      • @Eitan:对不起,我对此一无所知。 :(
      【解决方案3】:

      为此,也许使用 tabHost 不是正确的方法?

      【讨论】:

      • 您有什么替代方案?
      • 好吧,我正在考虑一种全局布局,其中包含隐藏模式下的所有选项卡 (View.GONE) 期望您想要显示的那个。但是您必须创建自己的包含选项卡标题的布局,以便当您想要取消选择所有选项卡时,您将能够在选项卡标题上设置所需的背景。这不是很干净,但它确实有效。希望你不必这样做;)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-03
      • 2021-12-22
      • 1970-01-01
      • 2012-06-05
      • 1970-01-01
      相关资源
      最近更新 更多