如何使用和动态填充多个 tabhost 视图

发布于 2024-12-23 08:19:07 字数 4389 浏览 2 评论 0原文

我需要创建一个活动选项卡主机,理想情况下创建选项卡和视图,并使用小部件动态填充视图。屏幕底部有一个“控制”按钮很重要。

我可以使用三种布局创建三个选项卡。让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云淡风轻 2024-12-30 08:19:07

我认为问题在于您试图将所有选项卡保留在一项活动下。您应该按如下方式将它们分开:

  • 使用处理 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:

  • Use a class that deals with the TabActivity
  • Create different Activities for each of your tabs
  • Add a tabhost layout

Check out this tutorial for help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文