简体   繁体   中英

Android dialog with ListView

I have a problem with a dialog with some buttons and a ListView.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:orientation="vertical"
tools:context=".MyApplicationActivity" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="@string/selectContactRecord"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="@android:color/white" />

<Button
    android:id="@+id/btnContactPickerAdd"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:drawableLeft="@android:drawable/ic_menu_add"
    android:drawableStart="@android:drawable/ic_menu_add"
    android:text="@string/noContact" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="15dp"
    android:text="@string/selectBulbs"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="@android:color/white" />

<ListView
    android:id="@+id/listViewCallRecordsDetail"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="@drawable/gradient"
    tools:listitem="@layout/callrecord_detail_list_item" >
</ListView>

<Button
    android:id="@+id/btnTestAddRecord"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="15dp"
    android:drawableLeft="@drawable/test_bulbs"
    android:drawableStart="@drawable/test_bulbs"
    android:text="@string/test" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="5dp"
    android:background="@android:color/black"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btnCancelAddRecord"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:drawableLeft="@android:drawable/ic_menu_revert"
        android:drawableStart="@android:drawable/ic_menu_revert"
        android:text="@string/cancel" />

    <Button
        android:id="@+id/btnSaveAddRecord"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:drawableLeft="@android:drawable/ic_menu_save"
        android:drawableStart="@android:drawable/ic_menu_save"
        android:text="@string/save" />
</LinearLayout>

The problem is now, if I have a lot of records in the ListView, the button below disappears and it is not possible to scroll down. I tried this with a ScrollView, but failed because it is not possible to use that with a ListView.

I want that the buttons below the listview always stay at the bottom and that the ListView is scrollable.

Can somebody please give me a hint?

Change your list view to this:

<ListView
android:id="@+id/listViewCallRecordsDetail"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:divider="@drawable/gradient"
tools:listitem="@layout/callrecord_detail_list_item" 

It'll make sure the button is layout in the bottom and, the list view take all the height remain

You want this :

I want that the buttons below the listview always stay at the bottom and that the ListView is scrollable.

For this change this:

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

    <TextView
        android:id="@+id/tvContactRecord"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/selectContactRecord"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@android:color/white" />

    <Button
        android:id="@+id/btnContactPickerAdd"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:drawableLeft="@android:drawable/ic_menu_add"
        android:drawableStart="@android:drawable/ic_menu_add"
        android:text="@string/noContact"
        android:layout_below="@+id/tvContactRecord" />

    <TextView
        android:id="@+id/tvSelectBulbs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="15dp"
        android:text="@string/selectBulbs"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@android:color/white"
        android:layout_below="@+id/btnContactPickerAdd" />

    <ListView
        android:id="@+id/listViewCallRecordsDetail"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="@drawable/gradient"
        tools:listitem="@layout/callrecord_detail_list_item"
        android:layout_below="@+id/tvSelectBulbs"
        android:layout_above="@+id/btnTestAddRecord" >
    </ListView>

    <Button
        android:id="@+id/btnTestAddRecord"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="15dp"
        android:drawableLeft="@drawable/test_bulbs"
        android:drawableStart="@drawable/test_bulbs"
        android:text="@string/test"
        android:layout_above="@+id/llBottom" />

    <LinearLayout
        android:id="@+id/llBottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="5dp"
        android:background="@android:color/black"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true" >

        <Button
            android:id="@+id/btnCancelAddRecord"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableLeft="@android:drawable/ic_menu_revert"
            android:drawableStart="@android:drawable/ic_menu_revert"
            android:text="@string/cancel" />

        <Button
            android:id="@+id/btnSaveAddRecord"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:drawableLeft="@android:drawable/ic_menu_save"
            android:drawableStart="@android:drawable/ic_menu_save"
            android:text="@string/save" />
    </LinearLayout>
</RelativeLayout>

You can consider below layout for the solution. You can replace Buttons as per your need.(I have given 2 buttons to simplify the things for you) Also you can also consider taking the buttons in another layout as well if required.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="My Button 2" />

    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/btn2"
        android:text="My Button 1" />

    <ListView
        android:id="@+id/lstMyListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/btn1" />

</RelativeLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM