[英]Android Layout with ListView between a “top bar” and “bottom bar”
我正在尝试构建一个布局,其中屏幕顶部有一个文本视图,屏幕底部有一个底部栏。 这些视图中的每一个都应保持固定,并且在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>
这是一个简单的建议。 制作一个新的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>
OPs布局的问题是第一个线性布局设置为android:layout_height="fill_parent"
,这将导致第一个线性布局填充父级。 它不知道您将添加更多视图。 您需要做的是按照知道其大小的顺序添加视图。 所以你知道顶部标准和底部android:layout_height="wrap_content"
的大小,你可以添加为android:layout_height="wrap_content"
。 然后添加listview,因为它的大小在编译时是未知的。
我使用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>
我遇到过这种问题。 我使用了三种布局而不是两种布局,这种解决方案:
并且最重要的是(你的问题):我将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>
我发现在封装在线性布局中时使用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>
我发现我认为这个帖子更容易解决
只需在视图列表中声明两个栏,在每种情况下添加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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.