我想让这个TabHost工作。当我将TabHost添加到xml布局并运行程序时,我可以看到每个选项卡的不同内容堆叠在一起。这告诉我布局工作正常。然后我将我的规范代码添加到活动中,然后在导航到此屏幕时关闭模拟器上的程序 所以这是我的XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tWelcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/bNewTask"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Task" />
<TabHost
android:id="@+id/myTabs"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/tTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
现在这是我的活动代码:(唯一重要的部分......)
welcomeName = (TextView) findViewById(R.id.tWelcome);
Test = (TextView) findViewById(R.id.tTest);
newTask = (Button) findViewById(R.id.bNewTask);
myTask = (ListView) findViewById(R.id.listView);
TabHost th = (TabHost) findViewById(R.id.myTabs);
welcomeName.setText("Welcome " + ToDoActivity.myUser.getName() + "!");
th.setup();
TabSpec specs = th.newTabSpec("tag1");
specs.setContent(R.id.tab1);
specs.setIndicator("All");
th.addTab(specs);
specs = th.newTabSpec("tag2");
specs.setContent(R.id.tab2);
specs.setIndicator("Personal");
th.addTab(specs);
答案 0 :(得分:0)
有几种可能性浮现在脑海中。
如果您使用TabActivity(不是要求,但更容易),则tabhost必须以android:id="@android:id/tabhost"
作为ID。
我也看到它说tabhost必须是根节点。我无法确认使用Android文档。在所有其他途径都失败后(或有人确认这是真的),我可能会做出这种改变。
如果使用TabActivity,则无需通过id引用tabhost,也无需使用setup()方法。我看到你正在使用setup()方法,但我可以告诉你,你的活动是否是TabActivity。如果是的话,也许这会导致你的问题。
以下是我用于标签布局设置的方法,它适用于我。请注意,这假定tabhost作为布局的根节点,并且它具有android:id/tabhost
作为id,并且它是TabActivity。
tabhost代码:
public class Tabs extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
spec = tabHost.newTabSpec("tag1").setIndicator("All").setContent(R.id.tab1);
tabHost.addTab(spec);
spec = tabHost.newTabSpec("tag2").setIndicator("Personal").setContent(R.id.tab2);
tabHost.addTab(spec);
}
}
希望这有帮助! (如果能解决问题,请务必单击复选标记以接受答案)。
答案 1 :(得分:0)
这是标签的Android教程页面: http://developer.android.com/resources/tutorials/views/hello-tabwidget.html
它有关于标签的信息和要求,它提供了大量的示例代码。您应该能够从那里进行复制粘贴,然后进行编辑。
具体来说,我将所有Tab元素的id更改为建议的Android ID:
android:id/tabhost
android:id/tabs
android:id/tabcontent