TabHost UI 布局问题

发布于 2024-09-05 02:27:40 字数 3862 浏览 1 评论 0原文

我想做的是创建一个像这样的布局:

       +--------+ 
Search |EditText| [Button]
       +--------+

+---------+
|Tab 1    |---------+
|         |Tab 2    |
|---------+---------+-----------------+
| ListView Item 1                     |
| ListView Item 2                     |
| ListView Item 3                     |
| ListView Item 4                     |
| ListView Item 5                     |
| ListView Item 6                     |
| ListView Item 7                     |
| ListView Item 8                     |
| ListView Item 9                     |
| ListView Item 10                    |
+-------------------------------------+    

这是一个显示“搜索”的 TextView、一个 EditText 和一个按钮。

然后在其下方是选项卡 - 选项卡 1 有一个 ListView,选项卡 2 有一些其他内容。我可以得到一个简单的 TabHost 设置,其中两个选项卡工作正常,但无法正确布局上面的内容。

问题是它安装和运行良好 - 只是布局错误。我尝试过加权视图等,但我无法按照上面的 ASCII 图来定位内容。

我将在 Eclipse 设计器中创建 UI 并从中检查 XML,但该设计器不能与 TabHost 一起使用。而且DroidDraw似乎不知道TabHost。

这是 XML:

<?xml version="1.0" encoding="utf-8"?>

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/TabHost"
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent">

        <LinearLayout 
            android:id="@+id/LinearLayout01" 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:padding="5dp">

            <TextView android:id="@+id/SearchTextView"
                android:text="Search"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="10">
            </TextView>

            <EditText 
                android:text="@+id/SearchEditText" 
                android:id="@+id/SearchEditText" 
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent"
                android:singleLine="true"
                android:layout_weight="10">
            </EditText>

            <TabWidget android:id="@android:id/tabs" 
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent"
                android:layout_weight="10"/>

            <FrameLayout android:id="@android:id/tabcontent" 
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent"
                android:layout_weight="70">

                <LinearLayout android:id="@+id/Tab1ListLayout" 
                    android:layout_width="fill_parent" 
                    android:layout_height="fill_parent">
                </LinearLayout>     

                <Button android:id="@+id/tab2"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="Tab 2 button"/>

            </FrameLayout>      
        </LinearLayout>
</TabHost>

...这是活动:

    public class tabtest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TabHost tabs = (TabHost)findViewById(R.id.TabHost);
        tabs.setup();
        TabHost.TabSpec spec = tabs.newTabSpec("tag1");
        spec.setContent(R.id.Tab1ListLayout);
        spec.setIndicator("Tab 1");
        tabs.addTab(spec);
        spec=tabs.newTabSpec("tag2");
        spec.setContent(R.id.tab2);
        spec.setIndicator("Tab 2");
        tabs.addTab(spec);

        EditText et = (EditText)findViewById(R.id.SearchEditText);
        et.setText("Some text.");

    }

What I'm trying to do is create a layout like this:

       +--------+ 
Search |EditText| [Button]
       +--------+

+---------+
|Tab 1    |---------+
|         |Tab 2    |
|---------+---------+-----------------+
| ListView Item 1                     |
| ListView Item 2                     |
| ListView Item 3                     |
| ListView Item 4                     |
| ListView Item 5                     |
| ListView Item 6                     |
| ListView Item 7                     |
| ListView Item 8                     |
| ListView Item 9                     |
| ListView Item 10                    |
+-------------------------------------+    

So that's a TextView saying 'Search', an EditText and a button.

Then below it the tabs - Tab 1 has a ListView, Tab 2 some other stuff. I can get a simple TabHost setup with two tabs working fine but can't get the above to lay out properly.

The issue is that that it installs and runs fine - just the layout is wrong. I've tried weighted views and so on but I can't get things to position as per the ASCII diagram above.

I'd create the UI in the Eclipse designer and check the XML from that, except the designer doesn't work with TabHost. And DroidDraw doesn't seem to know about TabHost.

Here's the XML:

<?xml version="1.0" encoding="utf-8"?>

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/TabHost"
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent">

        <LinearLayout 
            android:id="@+id/LinearLayout01" 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:padding="5dp">

            <TextView android:id="@+id/SearchTextView"
                android:text="Search"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="10">
            </TextView>

            <EditText 
                android:text="@+id/SearchEditText" 
                android:id="@+id/SearchEditText" 
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent"
                android:singleLine="true"
                android:layout_weight="10">
            </EditText>

            <TabWidget android:id="@android:id/tabs" 
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent"
                android:layout_weight="10"/>

            <FrameLayout android:id="@android:id/tabcontent" 
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent"
                android:layout_weight="70">

                <LinearLayout android:id="@+id/Tab1ListLayout" 
                    android:layout_width="fill_parent" 
                    android:layout_height="fill_parent">
                </LinearLayout>     

                <Button android:id="@+id/tab2"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="Tab 2 button"/>

            </FrameLayout>      
        </LinearLayout>
</TabHost>

... and here's the Activity:

    public class tabtest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TabHost tabs = (TabHost)findViewById(R.id.TabHost);
        tabs.setup();
        TabHost.TabSpec spec = tabs.newTabSpec("tag1");
        spec.setContent(R.id.Tab1ListLayout);
        spec.setIndicator("Tab 1");
        tabs.addTab(spec);
        spec=tabs.newTabSpec("tag2");
        spec.setContent(R.id.tab2);
        spec.setIndicator("Tab 2");
        tabs.addTab(spec);

        EditText et = (EditText)findViewById(R.id.SearchEditText);
        et.setText("Some text.");

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

ㄟ。诗瑗 2024-09-12 02:27:40

看来您需要回顾基础知识,例如 "layout resources" “声明布局”

请参阅布局技巧了解“权重”线性布局技巧。可能的解决方案:

<?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="fill_parent" android:layout_height="fill_parent">
    <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
        <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal">
            <TextView android:id="@+id/SearchTextView"
                android:text="Search"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            </TextView>

            <EditText 
                android:text="@+id/SearchEditText" 
                android:id="@+id/SearchEditText" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:singleLine="true">
            </EditText>

        </LinearLayout>

        <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="0dip" android:layout_weight="1"/>
             <LinearLayout android:id="@+id/Tab1ListLayout" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content">
            </LinearLayout>     

            <Button android:id="@+id/tab2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Tab 2 button"/>
    </LinearLayout>
</TabHost>

It seems you need to review basics such "layout resources" and "declaring layout".

See layout tricks for "weight" linear layout trick. Possible solution:

<?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="fill_parent" android:layout_height="fill_parent">
    <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
        <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal">
            <TextView android:id="@+id/SearchTextView"
                android:text="Search"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            </TextView>

            <EditText 
                android:text="@+id/SearchEditText" 
                android:id="@+id/SearchEditText" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:singleLine="true">
            </EditText>

        </LinearLayout>

        <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="0dip" android:layout_weight="1"/>
             <LinearLayout android:id="@+id/Tab1ListLayout" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content">
            </LinearLayout>     

            <Button android:id="@+id/tab2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Tab 2 button"/>
    </LinearLayout>
</TabHost>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文