【发布时间】:2011-12-21 01:55:58
【问题描述】:
我有一个包含 5 个标签的 TabHost。 据我所知,必须始终选择一个选项卡。
我需要一种方法来取消选择我的所有选项卡,这样就不会选择任何选项卡。
如果 tabhost 的一般意思是始终选择一个选项卡, 我怎样才能让它看起来(UI 说话)好像标签没有被选中?
【问题讨论】:
标签: android tabs android-tabhost unselect
我有一个包含 5 个标签的 TabHost。 据我所知,必须始终选择一个选项卡。
我需要一种方法来取消选择我的所有选项卡,这样就不会选择任何选项卡。
如果 tabhost 的一般意思是始终选择一个选项卡, 我怎样才能让它看起来(UI 说话)好像标签没有被选中?
【问题讨论】:
标签: android tabs android-tabhost unselect
试试这个:
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" 以显示它“未选中”,那么您可以使用此函数更改选项卡的颜色:
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>
【讨论】:
为此,也许使用 tabHost 不是正确的方法?
【讨论】:
View.GONE) 期望您想要显示的那个。但是您必须创建自己的包含选项卡标题的布局,以便当您想要取消选择所有选项卡时,您将能够在选项卡标题上设置所需的背景。这不是很干净,但它确实有效。希望你不必这样做;)