我有一个TabHost 5选项卡。在我看来,总是有一个选项卡被选中。
我需要一种方法来取消选择我所有的选项卡,所以没有一个将被选中。
如果tabhost的一般含义是始终选择一个选项卡,我怎么能使它出现(UI说话),如果选项卡没有被选中?
try this:
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);
这是不可能的。但是,是的,你可以设置所选选项卡的颜色看起来像它是未选中的,并设置一个空白的布局,通过管理一个全局变量,当你使它"未选中"和设置正常布局,当你想要它正常显示给用户。但这是一种技巧。
希望你明白我的意思!编辑:假设你在代码的某个地方设置了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);
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>
对于这个目的,也许使用tabHost不是正确的方法?