布局问题:如何在顶部和底部放置东西?

发布于 2024-10-11 14:31:18 字数 2377 浏览 3 评论 0原文

我想创建一个布局,顶部和底部有一个水平的 LinearLayout,中间有一个 ListView 填充。

我如何定义main.xml。

我尝试创建一个布局,顶部为水平 LinearLayout,底部为 TextView,中间填充为 ListView;没问题。但是当我将底部TextView修改为LinearLayout后,底部LinearLayout消失了。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    >
    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal"
        >
        <TextView 
            android:textSize="12px" 
            android:text="something here" 
            android:layout_width="50px" 
            android:layout_height="wrap_content"
            /> 
        <TextView 
            android:textSize="12px" 
            android:text="something here" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            />
    </LinearLayout> 

    <LinearLayout 
        android:orientation="vertical" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:gravity="bottom" 
        >
        <ListView 
            android:id="@+id/listbody" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            />
        <LinearLayout 
            android:orientation="horizontal" 
            android:layout_height="wrap_content" 
            android:layout_width="fill_parent" 
            > 
            <TextView 
                android:layout_height="wrap_content" 
                android:layout_width="0dip" 
                android:layout_weight="1" 
                android:textSize="12px" 
                android:text="50%" 
                /> 
            <TextView 
                android:layout_height="wrap_content" 
                android:layout_width="0dip" 
                android:layout_weight="1" 
                android:textSize="12px" 
                android:text="50%" 
                /> 
        </LinearLayout> 
    </LinearLayout> 
</LinearLayout>

有谁可以告诉一下建议吗? 请帮忙。

I want create a layout, with a horizontal LinearLayout(s) on top and bottom, a ListView fill in middle.

How can I define the main.xml.

I tried to create a layout with horizontal LinearLayout on top, TextView on bottom, a ListView fill in middle; is ok. But after I modified the bottom TextView to LinearLayout, the bottom LinearLayout disappear.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    >
    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal"
        >
        <TextView 
            android:textSize="12px" 
            android:text="something here" 
            android:layout_width="50px" 
            android:layout_height="wrap_content"
            /> 
        <TextView 
            android:textSize="12px" 
            android:text="something here" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            />
    </LinearLayout> 

    <LinearLayout 
        android:orientation="vertical" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:gravity="bottom" 
        >
        <ListView 
            android:id="@+id/listbody" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            />
        <LinearLayout 
            android:orientation="horizontal" 
            android:layout_height="wrap_content" 
            android:layout_width="fill_parent" 
            > 
            <TextView 
                android:layout_height="wrap_content" 
                android:layout_width="0dip" 
                android:layout_weight="1" 
                android:textSize="12px" 
                android:text="50%" 
                /> 
            <TextView 
                android:layout_height="wrap_content" 
                android:layout_width="0dip" 
                android:layout_weight="1" 
                android:textSize="12px" 
                android:text="50%" 
                /> 
        </LinearLayout> 
    </LinearLayout> 
</LinearLayout>

Anybody can tell advise?
Please help.

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

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

发布评论

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

评论(5

风筝在阴天搁浅。 2024-10-18 14:31:18

尝试将整个集合包含在相对布局中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/top_linear_layout_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" >
    </LinearLayout>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/bottom_linear_layout_id"
        android:layout_below="@id/top_linear_layout_id" />

    <LinearLayout
        android:id="@+id/bottom_linear_layout_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >
    </LinearLayout>
</RelativeLayout>

使用大小调整内容进行编辑。

Try containing the whole set in a RelativeLayout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/top_linear_layout_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" >
    </LinearLayout>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/bottom_linear_layout_id"
        android:layout_below="@id/top_linear_layout_id" />

    <LinearLayout
        android:id="@+id/bottom_linear_layout_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >
    </LinearLayout>
</RelativeLayout>

Edited with sizing stuff.

清君侧 2024-10-18 14:31:18

这就是您要找的东西。 Estel 使用RelativeLayout 走在正确的轨道上(虽然可以用LinearLayout 来完成,但我认为RelativeLayout 方法更干净)但是布局乱了没关系,检查Estel 的评论证明了我错误的。 :) 你应该首先定义你的标题,将其与顶部对齐;接下来,定义页脚,并将其与底部对齐;最后,声明你的 ListView,给它一个 fill_parent 的高度,并将其设置为布局在页脚上方和标题下方:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:alignParentTop="true"
        >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/footer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:alignParentBottom="true"
        >
    </LinearLayout>

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/footer"
        android:layout_below="@id/header"
        />
</RelativeLayout>

Here's what you're looking for. Estel's on the right track with the RelativeLayout (although it can be done with a LinearLayout, I think the RelativeLayout approach is cleaner, though) but the layout is out of order Nevermind, check Estel's comment which proved me wrong. :) You should first define your header, align it to the top; next, define your footer, and align it to the bottom; finally, declare your ListView, give it a height of fill_parent, and set it to layout above the footer and below the header:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:alignParentTop="true"
        >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/footer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:alignParentBottom="true"
        >
    </LinearLayout>

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/footer"
        android:layout_below="@id/header"
        />
</RelativeLayout>
莫言歌 2024-10-18 14:31:18

问题是您正在相对于它们所包含的内容的大小来定义它们,但是如果两者的组合内容大于屏幕,那么其中一个将必须与另一个重叠,您最好在线性布局中使用绝对测量值定义layout_height

The problem would be that you are defining both of them relative to the size of the content they contain, but if the combined content of both is bigger than the screen then one of them will have to overlap the other one, you would be better to define the layout_height using an absolute measurement in a linear layout

橘虞初梦 2024-10-18 14:31:18

感谢大家对RelativeLayout 的建议和指导。我现在可以修好它。

我必须在ListView之前定义页脚LinearLayout,并将ListView定义为 android:layout_alignParentTop="true" 和 android:layout_above="@id/footer"

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
    >
        <TextView 
            android:textSize="12px" 
            android:text="something here" 
            android:layout_width="50px" 
            android:layout_height="wrap_content"
            /> 
        <TextView 
            android:textSize="12px" 
            android:text="something here" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
        />
    </LinearLayout>
    <RelativeLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    >
        <LinearLayout
            android:id="@+id/footer"
            android:orientation="horizontal"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:layout_alignParentBottom="true"
        >

            <TextView 
                android:layout_height="wrap_content" 
                android:layout_width="0dip" 
                android:layout_weight="1" 
                android:textSize="12px" 
                android:text="50%" 
                /> 
            <TextView 
                android:layout_height="wrap_content" 
                android:layout_width="0dip" 
                android:layout_weight="1" 
                android:textSize="12px" 
                android:text="50%" 
            /> 

        </LinearLayout>
        <ListView
            android:id="@+id/tablebody"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_above="@id/footer"
        />
    </RelativeLayout>
</LinearLayout>

Thanks all for your advice and direction of RelativeLayout. I can fix it now.

I have to define the footer LinearLayout before the ListView, and define ListView as android:layout_alignParentTop="true" and android:layout_above="@id/footer"

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
    >
        <TextView 
            android:textSize="12px" 
            android:text="something here" 
            android:layout_width="50px" 
            android:layout_height="wrap_content"
            /> 
        <TextView 
            android:textSize="12px" 
            android:text="something here" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
        />
    </LinearLayout>
    <RelativeLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    >
        <LinearLayout
            android:id="@+id/footer"
            android:orientation="horizontal"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:layout_alignParentBottom="true"
        >

            <TextView 
                android:layout_height="wrap_content" 
                android:layout_width="0dip" 
                android:layout_weight="1" 
                android:textSize="12px" 
                android:text="50%" 
                /> 
            <TextView 
                android:layout_height="wrap_content" 
                android:layout_width="0dip" 
                android:layout_weight="1" 
                android:textSize="12px" 
                android:text="50%" 
            /> 

        </LinearLayout>
        <ListView
            android:id="@+id/tablebody"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_above="@id/footer"
        />
    </RelativeLayout>
</LinearLayout>
梦醒灬来后我 2024-10-18 14:31:18

这肯定会解决你的问题:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">
    <HorizontalScrollView
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:orientation="horizontal">
            <TextView 
                android:textSize="12sp" 
                android:text="something here" 
                android:layout_width="50dp" 
                android:layout_height="wrap_content"/> 
            <TextView 
                android:textSize="12sp" 
                android:text="something here" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content"/>
        </LinearLayout>
    </HorizontalScrollView>
    <ListView 
        android:id="@+id/listbody" 
        android:layout_width="fill_parent" 
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <HorizontalScrollView
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content">
        <LinearLayout 
            android:orientation="horizontal" 
            android:layout_height="wrap_content" 
            android:layout_width="fill_parent"> 
            <TextView 
                android:layout_height="wrap_content" 
                android:layout_width="0dip" 
                android:layout_weight="1" 
                android:textSize="12sp" 
                android:text="50%"/> 
            <TextView 
                android:layout_height="wrap_content" 
                android:layout_width="0dip" 
                android:layout_weight="1" 
                android:textSize="12sp" 
                android:text="50%"/> 
        </LinearLayout>
    </HorizontalScrollView>
</LinearLayout>

This will definitely solve your problem:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">
    <HorizontalScrollView
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:orientation="horizontal">
            <TextView 
                android:textSize="12sp" 
                android:text="something here" 
                android:layout_width="50dp" 
                android:layout_height="wrap_content"/> 
            <TextView 
                android:textSize="12sp" 
                android:text="something here" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content"/>
        </LinearLayout>
    </HorizontalScrollView>
    <ListView 
        android:id="@+id/listbody" 
        android:layout_width="fill_parent" 
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <HorizontalScrollView
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content">
        <LinearLayout 
            android:orientation="horizontal" 
            android:layout_height="wrap_content" 
            android:layout_width="fill_parent"> 
            <TextView 
                android:layout_height="wrap_content" 
                android:layout_width="0dip" 
                android:layout_weight="1" 
                android:textSize="12sp" 
                android:text="50%"/> 
            <TextView 
                android:layout_height="wrap_content" 
                android:layout_width="0dip" 
                android:layout_weight="1" 
                android:textSize="12sp" 
                android:text="50%"/> 
        </LinearLayout>
    </HorizontalScrollView>
</LinearLayout>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文