如何使用和动态填充多个 tabhost 视图
我需要创建一个活动选项卡主机,理想情况下创建选项卡和视图,并使用小部件动态填充视图。屏幕底部有一个“控制”按钮很重要。
我可以使用三种布局创建三个选项卡。让 EditTexts a) 出现,b) 在选项卡下方排列 [使用分隔符] 是一个挑战,StackOverflow 上现有答案提供了帮助。
我现在无法解决的是为什么第二个布局/选项卡上的 EditTexts 不出现,而第一个选项卡上的 EditTexts 却出现?
布局
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<View
android:id="@+id/separator"
android:layout_below="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="2dip" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_below="@+id/separator"
android:layout_above="@+id/btnSend"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/tabview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/ll1text"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="ll1text" />
</LinearLayout>
<LinearLayout
android:id="@+id/tabview2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/ll2text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="ll2text" />
</LinearLayout>
<RelativeLayout
android:id="@+id/tabview3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/rl3text"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:text="ll3text" />
</RelativeLayout>
</FrameLayout>
<Button
android:layout_alignParentBottom="true"
android:text="Start Travelling"
android:id="@+id/btnSend"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
</RelativeLayout>
</TabHost>
活动代码
setContentView(R.layout.template);
TabHost th = getTabHost();
th.addTab(th.newTabSpec("tab1").setIndicator("Customer").setContent(R.id.tabview1));
LinearLayout ll = (LinearLayout) findViewById(R.id.tabview1);
EditText et1 = new EditText (this);
et1.setText("ET1");
ll.addView(et1);
TextView tv2 = new TextView(this);
tv2.setText("et2:");
ll.addView(tv2);
EditText et2 = new EditText (this);
et2.setText("ET2");
ll.addView(et2);
th.addTab(th.newTabSpec("tab2").setIndicator("Job").setContent(R.id.tabview2));
ll = (LinearLayout) findViewById(R.id.tabview2);
EditText et21 = new EditText (this);
et21.setText("et21");
ll.addView(et21);
EditText et22 = new EditText (this);
et22.setText("et22");
ll.addView(et22);
EditText et23 = new EditText (this);
et23.setText("et23");
ll.addView(et23);
th.addTab(th.newTabSpec("tab3").setIndicator("Tab 3").setContent(R.id.tabview3));
RelativeLayout rl = (RelativeLayout) findViewById(R.id.tabview3);
EditText et3 = new EditText (this);
et3.setText("ET3");
rl.addView(et3);
th.setCurrentTab(0);
为什么第二个选项卡上的三个 EditText 不显示?
I need to create a single activity tabhost, ideally create tabs and views, and dynamically populate the views with widgets. Having a 'control' button at the bottom of the screen is important.
I am able to create three tabs, using three layouts. Getting the EditTexts to a) appear, and b) line up below the tabs [using the separator] was a challenge, assisted by existing answers on StackOverflow.
What I now cannot work out is why the EditTexts on the second layout/tab don't appear, whereas those on the first tab do?
The layout
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<View
android:id="@+id/separator"
android:layout_below="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="2dip" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_below="@+id/separator"
android:layout_above="@+id/btnSend"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/tabview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/ll1text"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="ll1text" />
</LinearLayout>
<LinearLayout
android:id="@+id/tabview2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/ll2text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="ll2text" />
</LinearLayout>
<RelativeLayout
android:id="@+id/tabview3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/rl3text"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:text="ll3text" />
</RelativeLayout>
</FrameLayout>
<Button
android:layout_alignParentBottom="true"
android:text="Start Travelling"
android:id="@+id/btnSend"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
</RelativeLayout>
</TabHost>
The activity code
setContentView(R.layout.template);
TabHost th = getTabHost();
th.addTab(th.newTabSpec("tab1").setIndicator("Customer").setContent(R.id.tabview1));
LinearLayout ll = (LinearLayout) findViewById(R.id.tabview1);
EditText et1 = new EditText (this);
et1.setText("ET1");
ll.addView(et1);
TextView tv2 = new TextView(this);
tv2.setText("et2:");
ll.addView(tv2);
EditText et2 = new EditText (this);
et2.setText("ET2");
ll.addView(et2);
th.addTab(th.newTabSpec("tab2").setIndicator("Job").setContent(R.id.tabview2));
ll = (LinearLayout) findViewById(R.id.tabview2);
EditText et21 = new EditText (this);
et21.setText("et21");
ll.addView(et21);
EditText et22 = new EditText (this);
et22.setText("et22");
ll.addView(et22);
EditText et23 = new EditText (this);
et23.setText("et23");
ll.addView(et23);
th.addTab(th.newTabSpec("tab3").setIndicator("Tab 3").setContent(R.id.tabview3));
RelativeLayout rl = (RelativeLayout) findViewById(R.id.tabview3);
EditText et3 = new EditText (this);
et3.setText("ET3");
rl.addView(et3);
th.setCurrentTab(0);
How come the three EditTexts on the second tab don't show?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题在于您试图将所有选项卡保留在一项活动下。您应该按如下方式将它们分开:
TabActivity
的类tabhost
布局查看 本教程寻求帮助。
I think the problem is that you are trying to hold all your tabs under one activity. You should separate them as follows:
TabActivity
tabhost
layoutCheck out this tutorial for help.