Android 布局,ListView 位于“顶栏”之间和“底栏”

发布于 2024-09-28 19:11:45 字数 2457 浏览 9 评论 0原文

我正在尝试构建一个布局,其中屏幕顶部有一个文本视图,屏幕底部有一个底部栏。 这些视图中的每一个都应该固定在适当的位置,并且在这两个视图之间应该是一个 ListView。

TOPBAR
LISTVIEW (scrollable)
BOTTOM BAR

我的布局(见下文)几乎有效:顶部和底部组件保持固定,列表视图滚动。 “问题”是我的 ListView 的最后一行仍然隐藏在底部栏后面。

关于如何调整布局有什么建议吗?

谢谢!

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
            android:layout_alignParentTop="true"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            >
        <TextView
                android:background="@drawable/blackgrad"
                android:gravity="center"
                android:id="@+id/title"
                android:layout_height="50dip"
                android:layout_width="fill_parent"
                android:text="blah blah"
                android:textColor="#ffffff"
                android:textSize="18sp"
                />
        <ListView
                android:background="#ffffff"
                android:cacheColorHint="#ffffffff"
                android:id="@android:id/list"
                android:layout_height="fill_parent"
                android:layout_width="fill_parent"
                />
    </LinearLayout>
    <LinearLayout
            android:background="#efefef"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_width="fill_parent"
            android:layout_height="50dip"
            android:orientation="horizontal">
        <Button
                android:layout_height="wrap_content"
                android:id="@+id/back"
                android:text="Back"
                android:layout_width="wrap_content" />

        <Button
                android:layout_height="wrap_content"
                android:id="@+id/home"
                android:text="Home"
                android:layout_width="wrap_content" />

        <Button
                android:layout_height="wrap_content"
                android:id="@+id/next"
                android:text="Next"
                android:layout_width="wrap_content" />


    </LinearLayout>
</RelativeLayout>

I'm trying to construct a layout where there is a text view at the top of the screen and a bottom bar at the bottom of the screen.
Each of these views should stay fixed in place and in between the 2 should be a ListView.

TOPBAR
LISTVIEW (scrollable)
BOTTOM BAR

My layout (see below) almost works: the top and bottom components stay fixed and the listview scrolls. The "problem" is that the final row of my ListView remains hidden behind the bottom bar.

Any suggestions on how to adjust the layout?

Thanks!

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
            android:layout_alignParentTop="true"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            >
        <TextView
                android:background="@drawable/blackgrad"
                android:gravity="center"
                android:id="@+id/title"
                android:layout_height="50dip"
                android:layout_width="fill_parent"
                android:text="blah blah"
                android:textColor="#ffffff"
                android:textSize="18sp"
                />
        <ListView
                android:background="#ffffff"
                android:cacheColorHint="#ffffffff"
                android:id="@android:id/list"
                android:layout_height="fill_parent"
                android:layout_width="fill_parent"
                />
    </LinearLayout>
    <LinearLayout
            android:background="#efefef"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_width="fill_parent"
            android:layout_height="50dip"
            android:orientation="horizontal">
        <Button
                android:layout_height="wrap_content"
                android:id="@+id/back"
                android:text="Back"
                android:layout_width="wrap_content" />

        <Button
                android:layout_height="wrap_content"
                android:id="@+id/home"
                android:text="Home"
                android:layout_width="wrap_content" />

        <Button
                android:layout_height="wrap_content"
                android:id="@+id/next"
                android:text="Next"
                android:layout_width="wrap_content" />


    </LinearLayout>
</RelativeLayout>

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

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

发布评论

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

评论(6

吻泪 2024-10-05 19:11:45

这里有一个简单的建议。创建一个新的 XML 并尝试一下。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <TextView
        android:text="Header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <TextView
        android:text="Footer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:layout_gravity="bottom"/>
</LinearLayout>

Here comes a simple suggestion. Make a new XML and have a go with this.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <TextView
        android:text="Header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <TextView
        android:text="Footer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:layout_gravity="bottom"/>
</LinearLayout>
梦醒灬来后我 2024-10-05 19:11:45

OP 布局的问题在于,第一个线性布局设置为 android:layout_height="fill_parent" ,这将导致第一个线性布局填充父级。它不知道您要添加更多视图。您要做的就是按照视图知道其大小的顺序添加视图。所以您知道顶部栏和底部栏的大小,您可以将它们添加为 android:layout_height="wrap_content"。然后添加列表视图,因为它的大小在编译时未知。

The problem with OPs layout is that the first linear layout is set as android:layout_height="fill_parent" which will cause the first linear layout to fill the parent. It doesn't know that you're going to add further views. What you have to do is add views in the order that they know their size. So you know the size of the top par and the bottom bar, you can add those as android:layout_height="wrap_content". Then add the listview since it's size is unknown at compile time.

青萝楚歌 2024-10-05 19:11:45

我使用 ID 和layout_below/layout_above 属性以类似的方式完成了您想要做的事情。 View的排列方式不同,但最终结果是相同的。一个位于另外两个 LinearLayout 之间的 ListView,一个在顶部,一个在底部。

ListView 中,您可以看到我有:

android:layout_above="@+id/event_list_buttons"
android:layout_below="@id/title_container"

这些属性将 ListView 定位在我的顶部和底部 LinearLayout 之间。

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

    <LinearLayout style="@style/TitleBar">
        <ImageView style="@style/TitleBarLogo" android:src="@drawable/logo_small_transparent" />

        <View style="@style/TitleBarSpring" />

        <ImageView style="@style/TitleBarSeparator" />
        <ImageButton style="@style/TitleBarAction" android:src="@drawable/ic_title_refresh"
            android:id="@+id/title_refresh_button" android:onClick="onClick" />
        <ProgressBar style="@style/TitleBarProgressIndicator"
            android:id="@+id/title_refresh_progress" android:visibility="gone" />

        <ImageView style="@style/TitleBarSeparator" />
        <ImageButton style="@style/TitleBarAction" android:src="@drawable/ic_title_search" />
    </LinearLayout>

    <ListView android:layout_width="fill_parent"
        android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/android:list" android:layout_above="@+id/event_list_buttons"
        android:layout_below="@id/title_container" android:fastScrollEnabled="true"
        android:background="@color/white"></ListView>

    <LinearLayout android:layout_width="fill_parent"
        xmlns:android="http://schemas.android.com/apk/res/android" android:id="@id/event_list_buttons"
        android:layout_height="wrap_content" android:layout_alignParentBottom="true"
        android:gravity="center_horizontal">

        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:id="@+id/events_list_view_organizing_button"
            android:text="Organizing" android:onClick="onClick"></Button>
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:id="@+id/events_list_view_attending_button"
            android:text="Attending" android:onClick="onClick"></Button>

    </LinearLayout>
</RelativeLayout>

I accomplished what you're trying to do in a similar way, using IDs and the layout_below/layout_above attributes. The Views are arranged in a different way, but the end result is the same. A ListView between two other LinearLayouts, one on top and one on bottom.

In the ListView you can see I have:

android:layout_above="@+id/event_list_buttons"
android:layout_below="@id/title_container"

Those attributes position the ListView between my top and bottom LinearLayouts.

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

    <LinearLayout style="@style/TitleBar">
        <ImageView style="@style/TitleBarLogo" android:src="@drawable/logo_small_transparent" />

        <View style="@style/TitleBarSpring" />

        <ImageView style="@style/TitleBarSeparator" />
        <ImageButton style="@style/TitleBarAction" android:src="@drawable/ic_title_refresh"
            android:id="@+id/title_refresh_button" android:onClick="onClick" />
        <ProgressBar style="@style/TitleBarProgressIndicator"
            android:id="@+id/title_refresh_progress" android:visibility="gone" />

        <ImageView style="@style/TitleBarSeparator" />
        <ImageButton style="@style/TitleBarAction" android:src="@drawable/ic_title_search" />
    </LinearLayout>

    <ListView android:layout_width="fill_parent"
        android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/android:list" android:layout_above="@+id/event_list_buttons"
        android:layout_below="@id/title_container" android:fastScrollEnabled="true"
        android:background="@color/white"></ListView>

    <LinearLayout android:layout_width="fill_parent"
        xmlns:android="http://schemas.android.com/apk/res/android" android:id="@id/event_list_buttons"
        android:layout_height="wrap_content" android:layout_alignParentBottom="true"
        android:gravity="center_horizontal">

        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:id="@+id/events_list_view_organizing_button"
            android:text="Organizing" android:onClick="onClick"></Button>
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:id="@+id/events_list_view_attending_button"
            android:text="Attending" android:onClick="onClick"></Button>

    </LinearLayout>
</RelativeLayout>
最偏执的依靠 2024-10-05 19:11:45

我有这样的问题。我使用了三个布局而不是两个,这种解决方案:

  • 3个布局存储在RelativeLayout中,如上面的解决方案中,
  • 包含列表视图的布局使用 android:layout_below 与“Topbar”(firstLayout)链接,
  • 第三个布局变为使用 android:layout_alignParentBottom="true"

    的“bottombar”

  • 以及最重要的事情(您的问题):我将 android:layout_marginBottom 添加到包含列表视图的布局中。在找到足够的余量之前,我必须对该值进行多次调整。

    <前><代码>

    <文本视图
    安卓:layout_width =“wrap_content”
    安卓:layout_height =“match_parent”
    机器人:layout_gravity =“center_vertical”
    机器人:layout_marginTop =“10dip”
    android:text="@string/show_history_text" />

    <线性布局
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/secondLayout"
    安卓:layout_width =“wrap_content”
    安卓:layout_height =“wrap_content”
    android:layout_below="@id/firstLayout"
    android:layout_marginBottom="50dip" >

    <列表视图
    android:id="@+id/ListNotes"
    安卓:layout_width =“fill_parent”
    android:layout_height="wrap_content" />

    <线性布局
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/thirdLayout"
    安卓:layout_width =“fill_parent”
    安卓:layout_height =“wrap_content”
    机器人:layout_alignParentBottom =“真”
    android:background="@color/LightSteelBlue"
    机器人:重力=“左”
    android:orientation="水平" >

    <按钮
    android:id="@+id/btnClose"
    安卓:layout_width =“fill_parent”
    安卓:layout_height =“wrap_content”
    机器人:layout_gravity =“底部”
    android:gravity="center_vertical|left"
    android:onClick="BtnCloseClick"
    android:text="@string/btn_close_history_text" //>

I had this kind of problem. I used three layouts instead of two, and this kind of solution :

  • the 3 layouts are stored in a RelativeLayout, as in solutions above
  • the layout containing the listview is linked with the "Topbar" (firstLayout) using android:layout_below
  • the third layout becomes the "bottombar" using android:layout_alignParentBottom="true"
  • and the most important thing (your problem) : I add android:layout_marginBottom to the layout containing the listview. I had to make several adjustement to the value before finding the sufficient margin.

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/firstLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:orientation="horizontal" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="center_vertical"
            android:layout_marginTop="10dip"
            android:text="@string/show_history_text" />
    </LinearLayout>
    
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/secondLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/firstLayout"
        android:layout_marginBottom="50dip" >
    
        <ListView
            android:id="@+id/ListNotes"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
    
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/thirdLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/LightSteelBlue"
        android:gravity="left"
        android:orientation="horizontal" >
    
        <Button
            android:id="@+id/btnClose"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:gravity="center_vertical|left"
            android:onClick="BtnCloseClick"
            android:text="@string/btn_close_history_text" />
    </LinearLayout>
    

叹倦 2024-10-05 19:11:45

当包含在线性布局中时,我找到了另一种方法来使用layout_weight来做到这一点。如果将 ListView 的权重设置为任意大的值,并将静态底部栏的权重设置为非常小的值,效果就会起作用。列表视图和底部栏的高度都必须设置为“wrap_content”才能使所有内容协同工作。

    <LinearLayout 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:orientation="vertical"
android:color="@color/bg"
xmlns:android="http://schemas.android.com/apk/res/android" >

    <ListView 
        android:id="@android:id/list" 
        android:padding="5dp"
        android:divider="@color/bg"
        android:dividerHeight="4dp"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_weight="20"
        >
    </ListView>

     <TextView android:text="Sorry, no results were found" android:id="@android:id/empty" android:layout_width="fill_parent" android:layout_height="fill_parent"></TextView>


    <include layout="@layout/bottom_bar"  android:layout_weight=".1" android:id="@+id/bottomBar" android:layout_alignParentBottom="true" android:layout_width="fill_parent" android:layout_height="wrap_content"></include>

I've found another way to do this with layout_weight's when enclosed in a linear layout. If you set the ListView's weight to something arbitrarily large and the static bottom bar's weight to something very small, the effect works. Both the list view and the bottom bar's height's must be set to "wrap_content" to make everything work together as well.

    <LinearLayout 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:orientation="vertical"
android:color="@color/bg"
xmlns:android="http://schemas.android.com/apk/res/android" >

    <ListView 
        android:id="@android:id/list" 
        android:padding="5dp"
        android:divider="@color/bg"
        android:dividerHeight="4dp"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_weight="20"
        >
    </ListView>

     <TextView android:text="Sorry, no results were found" android:id="@android:id/empty" android:layout_width="fill_parent" android:layout_height="fill_parent"></TextView>


    <include layout="@layout/bottom_bar"  android:layout_weight=".1" android:id="@+id/bottomBar" android:layout_alignParentBottom="true" android:layout_width="fill_parent" android:layout_height="wrap_content"></include>

最终幸福 2024-10-05 19:11:45

我发现我认为更简单的解决方案来自 这篇文章

只需在视图列表之前声明两个栏,在每种情况下添加 android:layout_alignParentBottom="true" 和 android:layout_alignParentTop="true" 。

然后在视图列表布局中指定它在它们之间设置的 android:layout_above="id_of_footer" 和 android:layout_below="id_of_header" 属性。

这是我的应用程序的工作示例。只是有页脚栏。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/bottom_bar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="#333333"
    android:padding="@dimen/padding_small" >

    <Button
        android:id="@+id/bt_add_session"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="addSession"
        android:text="@string/add_session" />

    <TextView
        android:id="@+id/lb_summary"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:text="@string/summary_example"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#FFFFFF" />
</LinearLayout>

<ListView
    android:id="@+id/vl_sessions"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/bottom_bar" >
</ListView>

I've found I think the easier solution just from this post

Just declare both bars BEFORE the viewlist adding android:layout_alignParentBottom="true" and android:layout_alignParentTop="true" in each case.

Then specify in the viewlist layout that it set between them with android:layout_above="id_of_footer" and android:layout_below="id_of_header" properties.

This is working example from my app. Just with footer bar.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/bottom_bar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="#333333"
    android:padding="@dimen/padding_small" >

    <Button
        android:id="@+id/bt_add_session"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="addSession"
        android:text="@string/add_session" />

    <TextView
        android:id="@+id/lb_summary"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:text="@string/summary_example"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#FFFFFF" />
</LinearLayout>

<ListView
    android:id="@+id/vl_sessions"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/bottom_bar" >
</ListView>

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