我试图在布局中制作标签.我发现了很多使用TabWidget,TabHost的示例和教程,但它们都涉及到以下之一:
>活动中的Java代码
>为每个选项卡分开活动
>为每个标签分隔碎片
标签内的内容是静态的,所以我应该能够将所有内容都包含在纯XML中.
反正这样做
解决方法
简单的答案,不.您必须在Java代码中设置您的TabHost并创建您的选项卡.您可以在不使用片段的情况下为选项卡提供静态布局,但仍需要使用Java进行安装.
如果您不在代码中执行此设置,您的TabWidget将不知道哪个布局对应于哪个选项卡,将无法正常工作.你将不得不写一点代码.
这样做的代码真的很简单.
XML(放置在你想要的布局中):
<TabHost android:id="@+id/tab_host" android:layout_width="fill_parent" android:layout_height="wrap_content" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/tab_one_container" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <LinearLayout android:id="@+id/tab_two_container" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </FrameLayout> </LinearLayout> </TabHost>
Java代码(放置在您设置布局的地方):
TabHost host = (TabHost)findViewById(R.id.tab_host); host.setup(); TabSpec spec = host.newTabSpec("Tab One"); spec.setContent(R.id.tab_one_container); spec.setIndicator("Tab One"); host.addTab(spec); spec = host.newTabSpec("Tab Two"); spec.setContent(R.id.tab_two_container); spec.setIndicator("Tab Two"); host.addTab(spec);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。