ListView drops to the bottom

和自甴很熟 提交于 2020-01-17 06:21:21

问题


I am using the following layout. Basically, it has a custom toolbar, a Navigation Drawer, a menu, a ListView (listView1), and adlay.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/background">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@color/header_bg"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/toolbar">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:layout_above="@+id/adlay"
            android:layout_margin="7dp"
            android:cacheColorHint="#00000000"
            android:divider="@android:color/transparent"
            android:dividerHeight="3dp">
        </ListView>
        <LinearLayout
            android:id="@+id/adlay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:orientation="vertical" >
        </LinearLayout>
    </RelativeLayout>

    <ListView
        android:id="@+id/drawer_list"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="#eee"
        android:dividerHeight="1dp"
        android:background="#fff"
        android:layout_below="@+id/toolbar"
        >
    </ListView>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>

The layout is kind of like the following order:

  1. Toolbar (with Navigation drawer and menu)
  2. listView1
  3. adlay

listView1 consists of a list of items loaded from external XML files.

Problem is this. If listView1's items is not much, then it drops to the bottom, sitting above adlay, and there is a gap above listView1, between the toolbar and listView1. The display is like the following:

  1. Toolbar (with Navigation drawer and menu)
  2. GAP! <--- I don't want the gap to be here!
  3. listView1
  4. adlay

This is not desired display.

Ideally, I want the following:

  1. Toolbar (with Navigation drawer and menu)
  2. listView1
  3. GAP should be here
  4. adlay

Any help is much appreciated. Thanks.


回答1:


i will write one layout file for your requirement. i hope it's usefull

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:color/white">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@android:color/black"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

        <android.support.v4.widget.DrawerLayout
            android:id="@+id/drawer_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/toolbar">

            <LinearLayout
                android:id="@+id/content_frame"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <ListView
                    android:id="@+id/listView1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="start"
                    android:layout_weight="1"
                    android:cacheColorHint="#00000000"
                    android:divider="@android:color/transparent"
                    android:dividerHeight="3dp">
                </ListView>

                <LinearLayout
                    android:id="@+id/adlay"
                    android:layout_marginTop="10dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Your Adlay Layout"/>
                </LinearLayout>
            </LinearLayout>

            <ListView
                android:id="@+id/drawer_list"
                android:layout_width="240dp"
                android:layout_height="match_parent"
                android:layout_below="@+id/toolbar"
                android:layout_gravity="start"
                android:background="#fff"
                android:choiceMode="singleChoice"
                android:divider="#eee"
                android:dividerHeight="1dp">
            </ListView>
        </android.support.v4.widget.DrawerLayout>
    </RelativeLayout>


来源:https://stackoverflow.com/questions/40380015/listview-drops-to-the-bottom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!