“取消选择” TabHost 中的选项卡

发布于 2024-12-13 08:33:13 字数 148 浏览 0 评论 0原文

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

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

如果选项卡主机的意思是始终选择一个选项卡, 我怎样才能让它看起来(用户界面)就像未选择该选项卡一样?

I have a TabHost holding 5 tabs.
As far as I can see, there has to be one tab selected at all times.

I need a way to unselect all my tabs so none will be selected.

If the tabhost is meant by general to have one tab selected at all times,
how can I make it appear (UI speaking) as if the tab isn't selected?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

以往的大感动 2024-12-20 08:33:13

试试这个:

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

try this:

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

or you can add hidden tab and select it when you want unselect a tab

tabHost.addTab(
            tabHost.newTabSpec("hiddenTab").setIndicator(""),
            MyFragment.class,
            null
    );

tabHost.getTabWidget().getChildTabViewAt(hiddenTabIndex).setVisibility(View.GONE);

and select this tab when you want

tabHost.setCurrentTab(hiddenTabIndex);
拿命拼未来 2024-12-20 08:33:13

据我所知这是不可能的。但是,是的,您可以将选定选项卡的颜色设置为看起来像未选定的,并通过在“未选定”时管理全局变量并在希望其正常向用户显示时设置正常布局来在其上设置空白布局。但这是一种伎俩。

希望,你明白我的意思!

编辑:

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

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

blank.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>

This is not possible AFAIK. but yes,you can set the selected tab's color to look like it is unselected and set a blank layout over it by managing a global variable when you make it 'unselected' and setting up normal layout when you want it to be shown normally to user. But this is kind of a trick.

Hope,you get my point!

EDIT :

Suppose you have set String what="disappear" somewhere in your code to show it 'unselected',then you can use this function to change color of tab:

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


    }

And in your activity class(which is opened by that selected tab):

FirstActivity.class:

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

blank.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>
音盲 2024-12-20 08:33:13

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

For this purpose, maybe using tabHost is not the proper way ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文