如何在Android中将列表视图放到底部

时间:2016-12-18 02:51:34

标签: android android-layout listview

我是Android新手,我面临很多布局问题。我的屏幕顶部有Toolbar。在此之下,我有一个ImageView,而ImageView下方有一个ListView。我在设置ListView高度时遇到了问题。由于我的ListView是屏幕上的最后一项,我希望它适合屏幕的其余区域。这是我想动态调整ListView的大小以使其适合。下面是我为屏幕设置的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.team.sidhesh.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="617dp"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorAccent"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

        <ImageView
            android:id="@+id/sliderTempImageView"
            android:layout_width="match_parent"
            android:layout_height="@dimen/slider_image_height"
            android:src="@drawable/slider_image1"
            android:scaleType="centerCrop"/>

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="@dimen/slider_seperator_height"
            android:src="@drawable/slider_seperator"
            android:scaleType="fitXY"/>

        <ListView
            android:id="@+id/list_view"
            android:background="@android:color/white"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true">

        </ListView>

    </android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

2 个答案:

答案 0 :(得分:1)

这取决于您的视图的父ViewGroup的类型。

对于RelativeLayout的父母,Jeetendra Choudhary对android:layout_alignParentBottom="true"的回答会很好。

对于LinearLayout父级,您应该让您的视图成为布局的最后一个子级,并设置android:layout_weight="1"以使其填充到底部的空间。

对于FrameLayout父级,您应在视图上设置android:layout_gravity="bottom"

您不应使用AppBarLayout作为根ViewGroup。我推荐这样的内容,LinearLayoutFrameLayout作为根ViewGroup

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

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorAccent"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

        <ImageView
            android:id="@+id/sliderTempImageView"
            android:layout_width="match_parent"
            android:layout_height="@dimen/slider_image_height"
            android:src="@drawable/slider_image1"
            android:scaleType="centerCrop"/>

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="@dimen/slider_seperator_height"
            android:src="@drawable/slider_seperator"
            android:scaleType="fitXY"/>

    </android.support.design.widget.AppBarLayout>

    <ListView
        android:id="@+id/list_view"
        android:background="@android:color/white"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

此外,ListView已过时。我建议改为使用支持库中的RecyclerViewhttps://developer.android.com/reference/android/support/v7/widget/RecyclerView.html

答案 1 :(得分:0)

您只需将ListView放在LinearLayout高度为match_parent并将gravity设置为bottom的范围内即可。像这样的东西,

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom">

    <ListView
        android:id="@+id/list_view"
        android:background="@android:color/white"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>