'Unselect' a tab in a TabHost

ぐ巨炮叔叔 提交于 2019-12-04 19:33:22

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>

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!