android Dialog 添加 ListView 自適應(yīng)高度

在自定義 Dialog 中添加一個ListView后,當條目過多時框杜,會把下方的控件擠出屏幕浦楣。
為了看到效果,我創(chuàng)建了一個非常簡單的 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>

運行截圖:


底部的控件被擠出顯示區(qū)域

問題解決

顯示底部控件
為了讓底部的控件顯示出來咪辱,要從 Dialog 的布局文件入手振劳。
設(shè)置 ListView 的權(quán)重,讓它后于底部控件計算高度油狂。

<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);
    }

這里我把高度設(shè)置成了屏幕高度的0.6倍,可以根據(jù)需要調(diào)整為其他倍數(shù)或固定值夹供。
最終 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>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末灵份,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子哮洽,更是在濱河造成了極大的恐慌填渠,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件鸟辅,死亡現(xiàn)場離奇詭異氛什,居然都是意外死亡,警方通過查閱死者的電腦和手機匪凉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進店門枪眉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人再层,你說我怎么就攤上這事贸铜。” “怎么了聂受?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵蒿秦,是天一觀的道長。 經(jīng)常有香客問我蛋济,道長棍鳖,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任碗旅,我火速辦了婚禮渡处,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘祟辟。我一直安慰自己医瘫,他們只是感情好,可當我...
    茶點故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布川尖。 她就那樣靜靜地躺著登下,像睡著了一般。 火紅的嫁衣襯著肌膚如雪叮喳。 梳的紋絲不亂的頭發(fā)上被芳,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天撇他,我揣著相機與錄音卖丸,去河邊找鬼。 笑死晶乔,一個胖子當著我的面吹牛锣咒,可吹牛的內(nèi)容都是我干的侵状。 我是一名探鬼主播赞弥,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼趣兄!你這毒婦竟也來了绽左?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤艇潭,失蹤者是張志新(化名)和其女友劉穎拼窥,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蹋凝,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡鲁纠,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了鳍寂。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片改含。...
    茶點故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖迄汛,靈堂內(nèi)的尸體忽然破棺而出捍壤,到底是詐尸還是另有隱情,我是刑警寧澤隔心,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布白群,位于F島的核電站,受9級特大地震影響硬霍,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜笼裳,卻給世界環(huán)境...
    茶點故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一唯卖、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧躬柬,春花似錦拜轨、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至颠锉,卻和暖如春法牲,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背琼掠。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工拒垃, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人瓷蛙。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓悼瓮,卻偏偏與公主長得像戈毒,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子横堡,可洞房花燭夜當晚...
    茶點故事閱讀 44,713評論 2 354

推薦閱讀更多精彩內(nèi)容