美文网首页Android开发Android开发学习Android知识
android Dialog 添加 ListView 自适应高度

android Dialog 添加 ListView 自适应高度

作者: sunrain_ | 来源:发表于2016-11-13 22:46 被阅读1922次

    在自定义 Dialog 中添加一个ListView后,当条目过多时,会把下方的控件挤出屏幕。
    为了看到效果,我创建了一个非常简单的 Dialog.

    public class ListViewDialog extends Dialog {
    
        private final Context mContext;
        private ListView mListView;    
    
        public ListViewDialog(Context context) {
            super(context);
            mContext = context;
            initView();
            initListView();
        }
    
        private void initView() {
            View contentView = View.inflate(mContext, R.layout.content_dialog, null);
            mListView = (ListView) contentView.findViewById(R.id.lv);
            setContentView(contentView);
        }
    
        private void initListView() {
            ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(mContext, android.R.layout.simple_expandable_list_item_1);
            for (int i = 0; i < 10; i++) {
                stringArrayAdapter.add("item " + i);
            }
            mListView.setAdapter(stringArrayAdapter);
        }
    }
    

    Dialog 的布局文件 content_dialog.xml

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:padding="15dp"
            android:text="带有ListView的Dialog"
            android:textColor="@android:color/black"
            android:textSize="18sp"/>
    
        <ListView
            android:id="@+id/lv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal"
            android:padding="10dp">
    
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="取消"/>
    
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_weight="1"
                android:text="确认"/>
        </LinearLayout>
    </LinearLayout>
    

    运行截图:


    底部的控件被挤出显示区域

    问题解决

    显示底部控件
    为了让底部的控件显示出来,要从 Dialog 的布局文件入手。
    设置 ListView 的权重,让它后于底部控件计算高度。

    <ListView
        android:id="@+id/lv"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    

    这样底部的控件就显示出来了:


    修改 Dialog 的最大高度
    ListView 也会把 Dialog 撑的很高。
    修改 Dialog 的高度可以重写 onWindowFocusChanged 方法。
        @Override
        public void onWindowFocusChanged(boolean hasFocus) {
            if (!hasFocus) {
                return;
            }
            setHeight();
        }
    
        private void setHeight() {
            Window window = getWindow();
            DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics();
            WindowManager.LayoutParams attributes = window.getAttributes();
            if (window.getDecorView().getHeight() >= (int) (displayMetrics.heightPixels * 0.6)) {
                attributes.height = (int) (displayMetrics.heightPixels * 0.6);
            }
            window.setAttributes(attributes);
        }
    

    这里我把高度设置成了屏幕高度的0.6倍,可以根据需要调整为其他倍数或固定值。
    最终 Dialog 效果如下:


    最终代码:

    public class ListViewDialog extends Dialog {
    
        private final Context mContext;
        private ListView mListView;
    
        public ListViewDialog(Context context) {
            super(context);
            mContext = context;
            initView();
            initListView();
        }
    
        private void initView() {
            View contentView = View.inflate(mContext, R.layout.content_dialog, null);
            mListView = (ListView) contentView.findViewById(R.id.lv);
            setContentView(contentView);
        }
    
        private void initListView() {
            ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(mContext, android.R.layout.simple_expandable_list_item_1);
            for (int i = 0; i < 10; i++) {
                stringArrayAdapter.add("item " + i);
            }
            mListView.setAdapter(stringArrayAdapter);
        }
    
        @Override
        public void onWindowFocusChanged(boolean hasFocus) {
            if (!hasFocus) {
                return;
            }
            setHeight();
        }
    
        private void setHeight() {
            Window window = getWindow();
            DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics();
            WindowManager.LayoutParams attributes = window.getAttributes();
            if (window.getDecorView().getHeight() >= (int) (displayMetrics.heightPixels * 0.6)) {
                attributes.height = (int) (displayMetrics.heightPixels * 0.6);
            }
            window.setAttributes(attributes);
        }
    }
    
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:padding="15dp"
            android:text="带有ListView的Dialog"
            android:textColor="@android:color/black"
            android:textSize="18sp"/>
    
        <ListView
            android:id="@+id/lv"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"/>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal"
            android:padding="10dp">
    
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="取消"/>
    
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_weight="1"
                android:text="确认"/>
        </LinearLayout>
    </LinearLayout>
    

    相关文章

      网友评论

      • CROAD:测量dialog高度,这个比较新颖

      本文标题:android Dialog 添加 ListView 自适应高度

      本文链接:https://www.haomeiwen.com/subject/qzgcpttx.html