开发者

Custom dialog with listview and button always fullscreen

I want to create a custom dialog that contains a listview and buttons below the listview. The buttons should be below the listview but always visible (layout_alignParentBottom="true").

I have created an xml that works quite well, but only on long lists. If the list is short, the buttons are still at the screen bottom, and the height of the dialog title is streched to make dialog fill screen. Image attached shows what I get today.

In short. I want a normal dialog that only fills screen if nececcary.

I am unable to post the xml for some reason. but I use a relativelayout, buttons are aligned parent bottom, small panel layouted above buttons, listview layouted above th开发者_Python百科is panel. All layouts are layout:height=wrap_content.

Thankful for any help. /Magnus

Custom dialog with listview and button always fullscreen


This answer is generally correct, but introduces a significant overhead, because of many nested layouts.

What you need is a simple LinearLayout with a specific configuration:

<ListView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
     />
<!-- Notice layout_weight=1 and height=0dp - it's the most important thing here-->


<WhateverLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
<!-- And the layout below the listview has some arbitrary height (but no weight!)-->

</WhateverLayout>

</LinearLayout>

The result is the same: "a dialog that is no bigger than the content and max as large as the screen, always buttons visible."


The problem is solved.

I took the alert_dialog.xml from the android source code and modified it a little. I added my listview to the customPanel framelayout, removed the topPanel and contentPanel.

Result: a dialog that is no bigger than the content and max as large as the screen, always buttons visible.


In answer to @Fresh_Meat

you can add the buttons to the bottom of a listview like this: http://www.anddev.org/code-snippets-for-android-f33/listview-with-a-footer-item-t49281.html

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:padding="10dip"
   >
    <LinearLayout
        android:id="@+id/bottom_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">
            <!--Put whatever view item you want here -->
            <EditText
             android:id="@+id/conversation_reply"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:text="write a reply..."
             />
   </LinearLayout>
   <ListView
    android:id="@+id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:cacheColorHint="#00000000"
    android:layout_above="@id/bottom_view"
    />
</RelativeLayout>

Custom dialog with listview and button always fullscreen

Use this within your Alert View

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜