如何在Android的AlertDialog中设置listview的最大高度?

如何在Android的AlertDialog中设置listview的最大高度?,android,listview,Android,Listview,我有一个AsyncTask,它可以配置多个设备,但其中一些设备可能会失败。当任务完成尝试配置所有设备时,它会显示一个包含所有失败设备的AlertDialog。问题是当很多设备因为listview太大而出现故障时,我的对话框按钮就消失了 使用listView创建对话框的代码: if (listInitialSettingsAdapter.getNerasList("fail").size() != 0) { AlertDialog.Builder dialog = new

我有一个AsyncTask,它可以配置多个设备,但其中一些设备可能会失败。当任务完成尝试配置所有设备时,它会显示一个包含所有失败设备的AlertDialog。问题是当很多设备因为listview太大而出现故障时,我的对话框按钮就消失了

使用listView创建对话框的代码:

    if (listInitialSettingsAdapter.getNerasList("fail").size() != 0) {
        AlertDialog.Builder dialog = new AlertDialog.Builder(activity);
        ArrayList<ScanResult> listFailedNeras = listInitialSettingsAdapter.getNerasList("fail");
        ArrayList<String> listFailedNerasString = new ArrayList<>();
        for (ScanResult neras :
                listFailedNeras) {
            listFailedNerasString.add(neras.SSID);
        }
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(activity, android.R.layout.simple_list_item_1, listFailedNerasString);
        ListView listView = new ListView(activity);
        listView.setAdapter(arrayAdapter);

        dialog.setView(listView)
                .setTitle("Neras não configurados")
                .setMessage("Os Neras listados abaixo não foram configurados, deseja tentar novamente?")
                .setSingleChoiceItems(arrayAdapter, 0, null)
                .setPositiveButton("SIM", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        InstalacaoInicialActivity.configureNerasTask = new ConfigureNerasTask(activity, listInitialSettingsAdapter.getNerasList("fail"), user, wifi, hashMapTimeZone, handler, showMeasures, listInitialSettingsAdapter);
                        InstalacaoInicialActivity.configureNerasTask.execute();
                        listInitialSettingsAdapter.updateViewWhenTryToConfigAgain(listInitialSettingsAdapter.getNerasList("fail"));
                    }
                })
                .setNegativeButton("NÂO", null)
                .show();
    }
if(listInitialSettingsAdapter.getNerasList(“失败”).size()!=0){
AlertDialog.Builder dialog=新建AlertDialog.Builder(活动);
ArrayList listFailedNeras=listInitialSettingsAdapter.getNerasList(“失败”);
ArrayList listFailedNerasString=new ArrayList();
对于(扫描结果):
列表故障(NERAS){
listFailedNerasString.add(neras.SSID);
}
ArrayAdapter ArrayAdapter=新的ArrayAdapter(activity,android.R.layout.simple\u list\u item\u 1,listFailedNerasString);
ListView ListView=新的ListView(活动);
setAdapter(arrayAdapter);
dialog.setView(listView)
.setTitle(“Neras não configurados”)
.setMessage(“新能源清单是一种新能源,是一种新能源吗?”)
.setSingleChoiceItems(arrayAdapter,0,null)
.setPositiveButton(“SIM”,新的DialogInterface.OnClickListener()对话框){
@凌驾
public void onClick(DialogInterface dialog,int which){
InstalacaoInialActivity.configureNerasTask=新配置的Nerastask(activity,listInitialSettingsAdapter.getNerasList(“失败”),用户,wifi,hashMapTimeZone,handler,showMeasures,listInitialSettingsAdapter);
InstalacaoInialActivity.configureNerasTask.execute();
listInitialSettingsAdapter.UpdateViewHentryToConfig(listInitialSettingsAdapter.getNerasList(“失败”);
}
})
.setNegativeButton(“否”,空)
.show();
}
我想做的很简单,我想限制listView的高度并在其中滚动,以查看所有出现故障的设备,并保持对话框的按钮不可触摸


我该怎么做

我建议您使用constraintlayout来指示列表的最大高度,并将按钮附加到屏幕底部

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

       <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"/>

      <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

</android.support.constraint.ConstraintLayout>

我建议您使用constraintlayout来指示列表的最大高度,并将按钮连接到屏幕底部

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

       <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"/>

      <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

</android.support.constraint.ConstraintLayout>


为什么要在ConstraintLayout内创建LinearLayout?我可以添加带有列表和按钮的ConstraintLayout吗?当然,用户刚才有一个问题:如何将按钮放在ListView上?为什么在ConstraintLayout内创建LinearLayout?我可以添加带有列表和按钮的ConstraintLayout吗?当然,用户刚才有一个问题,如何将按钮放在ListView上