在“顶栏”和“底栏”之间使用ListView的Android布局

问题描述 投票:12回答:6

我正在尝试构建一个布局,其中屏幕顶部有一个文本视图,屏幕底部有一个底部栏。这些视图中的每一个都应保持固定,并且在2之间应该是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>
android android-layout android-linearlayout footer android-relativelayout
6个回答
28
投票

这是一个简单的建议。制作一个新的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>

5
投票

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


4
投票

我使用ID和layout_below / layout_above属性以类似的方式完成了您尝试做的事情。 Views以不同的方式排列,但最终结果是相同的。两个ListViews之间的LinearLayout,一个在顶部,一个在底部。

ListView你可以看到我有:

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

这些属性将ListView定位在我的顶部和底部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>

3
投票

我遇到过这种问题。我使用了三种布局而不是两种布局,这种解决方案:

  • 3个布局存储在RelativeLayout中,如上面的解决方案
  • 包含listview的布局使用android:layout_below与“Top Bar”(第一个Layout)链接
  • 第三个布局使用android成为“底栏”:layout_alignParentBottom =“true”
  • 并且最重要的是(你的问题):我将android:layout_marginBottom添加到包含listview的布局中。在找到足够的保证金之前,我必须对该值进行几次调整。 <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>

2
投票

我发现在封装在线性布局中时使用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>


1
投票

我发现我认为更简单的解决方案来自this帖子

只需在视图列表中声明两个栏,在每种情况下添加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>

© www.soinside.com 2019 - 2024. All rights reserved.