你好,TabWidget每个选项卡都引用新的xml
我正在使用 Google 的 Hello, TabWidget 的 exmaple,但对其进行了更改看起来像这样:
main.xml:
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<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="fill_parent">
<TextView
android:text="@+layout/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="@+id/textview2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is another tab" />
<TextView
android:id="@+id/textview3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is a third tab" />
</FrameLayout>
</LinearLayout>
</TabHost>
java file:
public class HelloTabWidget extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 1").setContent(R.layout.text));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB 2").setContent(R.id.textview2));
mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("TAB 3").setContent(R.id.textview3));
mTabHost.setCurrentTab(0);
}
}
这里是 res/layout 中的 text.xml:
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="This is Tab 1" />
</LinearLayout>
我基本上想做的是让每个选项卡引用它自己的 xml 文件,而不是 main.xml 中的所有文件,但第一个选项卡中的文本不显示。
I'm using Google's exmaple of Hello, TabWidget but altered it to look like this:
main.xml:
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<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="fill_parent">
<TextView
android:text="@+layout/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="@+id/textview2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is another tab" />
<TextView
android:id="@+id/textview3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is a third tab" />
</FrameLayout>
</LinearLayout>
</TabHost>
java file:
public class HelloTabWidget extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 1").setContent(R.layout.text));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB 2").setContent(R.id.textview2));
mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("TAB 3").setContent(R.id.textview3));
mTabHost.setCurrentTab(0);
}
}
and here is the text.xml in res/layout:
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="This is Tab 1" />
</LinearLayout>
What I'm basically trying to do is have each tab refer to its own xml file rather than all in main.xml, but the text in the first tab doesn't show up.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否想让每个选项卡引用其自己的
Activity
?如果是这样,您可以设置一个意图作为每个选项卡的内容:当然,您必须有一个名为 Test(或其他任何名称)的类,将
test.xml
设置为布局。本教程将为您提供进一步的帮助(如果是这种情况)您正在尝试设置的。
Are you trying to have each tab refer to its own
Activity
? If so, you can set up an intent as the content for each tab:Of course, you'd then have to have a class named Test (or whatever else) that sets
test.xml
as the layout.This tutorial will help you further if this is the type of scenario that you're trying to set up.
使用内置选项卡主机的选项卡有三个选项。
听起来您想要#3。你需要做的是这样的:
There's three options for tabs using the built-in tab host.
It sounds like you want #3. What you need to do is something like this:
这不太正确,因为 LayoutInflater.inflate 有两个参数。
That's not quite right as LayoutInflater.inflate takes two parameters.