与TabHost UI布局问题 [英] Problem with TabHost UI layout

查看:142
本文介绍了与TabHost UI布局问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  + -------- +
搜索|的EditText | [按钮]
       + -------- +

+ --------- +
|标签1 | --------- +
| |标签2 |
| --------- + --------- + -------- +
| ListView项1 |
| ListView项2 |
| ListView项3 |
| ListView的项目4 |
| ListView的项目5 |
| ListView项6 |
| ListView项7 |
| ListView项8 |
| ListView项9 |
| ListView项10 |
+ ------------------------------------- +
 

所以这是一个TextView说'搜索',一个EditText和一个按钮。

然后在它下面的标签 - 标签1有一个ListView,标签2一些其他的东西。我可以得到一个简单的TabHost设置有两个选项卡工作正常,但不能得到上面的铺陈正常。

的问题是,它安装并正常运行 - 只要布局是错误的。我试过加权意见等,但我不能把事情位置按上面的ASCII图。

我想创建在Eclipse设计的用户界面,并从检查XML,但设计师并没有与TabHost工作。而DroidDraw似乎不知道TabHost。

这里的XML:

 < XML版本=1.0编码=UTF-8&GT?;

< TabHost的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:ID =@ + ID / TabHost
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT>

        <的LinearLayout
            机器人:ID =@ + ID / LinearLayout01
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =FILL_PARENT
            机器人:方向=垂直
            机器人:填充=5DP>

            < TextView的机器人:ID =@ + ID / SearchTextView
                机器人:文本=搜索
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =FILL_PARENT
                机器人:layout_weight =10>
            < / TextView的>

            <的EditText
                机器人:文本=@ + ID / SearchEditText
                机器人:ID =@ + ID / SearchEditText
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =FILL_PARENT
                机器人:单线=真
                机器人:layout_weight =10>
            < /的EditText>

            < TabWidget机器人:ID =@机器人:ID /标签
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =FILL_PARENT
                机器人:layout_weight =10/>

            <的FrameLayout机器人:ID =@机器人:ID / tabcontent
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =FILL_PARENT
                机器人:layout_weight =70>

                <的LinearLayout机器人:ID =@ + ID / Tab1ListLayout
                    机器人:layout_width =FILL_PARENT
                    机器人:layout_height =FILL_PARENT>
                < / LinearLayout中>

                <按钮机器人:ID =@ + ID / TAB2
                    机器人:layout_width =FILL_PARENT
                    机器人:layout_height =FILL_PARENT
                    机器人:文本=标签2键/>

            < /的FrameLayout>
        < / LinearLayout中>
< / TabHost>
 

......这里的活动:

 公共类tabtest延伸活动{
    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        TabHost标签=(TabHost)findViewById(R.id.TabHost);
        tabs.setup();
        TabHost.TabSpec规格= tabs.newTabSpec(标签1);
        spec.setContent(R.id.Tab1ListLayout);
        spec.setIndicator(标签1);
        tabs.addTab(规范);
        规格= tabs.newTabSpec(标签2);
        spec.setContent(R.id.tab2);
        spec.setIndicator(标签2);
        tabs.addTab(规范);

        的EditText等=(EditText上)findViewById(R.id.SearchEditText);
        et.setText(一些文字。);

    }
 

解决方案

看来你需要查看基础知识,的布局资源和的声明布局

请参阅布局窍门中的权重线性布局的把戏。可能的解决方法:

 < XML版本=1.0编码=UTF-8&GT?;
< TabHost的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android机器人:ID =@机器人:ID / tabhost机器人:layout_width =FILL_PARENT机器人:layout_height =FILL_PARENT >
    <的LinearLayout机器人:方向=垂直机器人:layout_width =FILL_PARENT机器人:layout_height =FILL_PARENT>
        < LinearLayout中的android:layout_width =FILL_PARENT机器人:layout_height =WRAP_CONTENT机器人:方向=横向>
            < TextView的机器人:ID =@ + ID / SearchTextView
                机器人:文本=搜索
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT>
            < / TextView的>

            <的EditText
                机器人:文本=@ + ID / SearchEditText
                机器人:ID =@ + ID / SearchEditText
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:单线=真正的>
            < /的EditText>

        < / LinearLayout中>

        < TabWidget机器人:ID =@机器人:ID /标签机器人:layout_width =FILL_PARENT机器人:layout_height =WRAP_CONTENT/>

        <的FrameLayout机器人:ID =@机器人:ID / tabcontent机器人:layout_width =FILL_PARENT机器人:layout_height =0dip机器人:layout_weight =1/>
             <的LinearLayout机器人:ID =@ + ID / Tab1ListLayout
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =WRAP_CONTENT>
            < / LinearLayout中>

            <按钮机器人:ID =@ + ID / TAB2
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:文本=标签2键/>
    < / LinearLayout中>
< / TabHost>
 

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

    }

解决方案

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>

这篇关于与TabHost UI布局问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆