我有一个 TabHost 持有 5 个标签。据我所知,必须始终选择一个选项卡。
我需要一种取消选择所有选项卡的方法,因此不会选择任何选项卡。
如果 tabhost 的意思是始终选择一个选项卡,我怎样才能让它看起来(UI 说)好像该选项卡没有被选中?
我有一个 TabHost 持有 5 个标签。据我所知,必须始终选择一个选项卡。
我需要一种取消选择所有选项卡的方法,因此不会选择任何选项卡。
如果 tabhost 的意思是始终选择一个选项卡,我怎样才能让它看起来(UI 说)好像该选项卡没有被选中?
试试这个:
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);
这是不可能的AFAIK。但是,是的,您可以将选定选项卡的颜色设置为看起来未选中,并通过在将其设为“未选中”时管理全局变量并在希望将其正常显示给用户时设置正常布局来在其上设置空白布局. 但这是一种诡计。
希望你明白我的意思!
编辑 :
假设您String what="disappear"
在代码中设置了某处以显示它“未选中”,那么您可以使用此功能更改选项卡的颜色:
主类:
//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>
为此,也许使用 tabHost 不是正确的方法?