TabHost UI布局的问题

Ala*_*n B 5 android android-tabhost

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

       +--------+ 
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                    |
+-------------------------------------+    
Run Code Online (Sandbox Code Playgroud)

所以这是一个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>
Run Code Online (Sandbox Code Playgroud)

......这是活动:

    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.");

    }
Run Code Online (Sandbox Code Playgroud)

Bak*_*yor 2

看来您需要复习“布局资源”“声明布局”等基础知识。

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

<?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>
Run Code Online (Sandbox Code Playgroud)