![](/img/trans.png)
[英]Android:How can I set the AlertDialog width and height,and the button of the AlertDialog style?
[英]How can I set the max height to a listview inside an AlertDialog in Android?
我有一个配置多个设备的 AsyncTask,但是其中一些可能会失败,当任务完成尝试配置所有设备时,它会显示一个 AlertDialog,其中包含所有失败的设备。 问题是当很多设备失败时,因为列表视图变得如此之大,以至于我的对话框按钮消失了。
使用 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();
}
我想做的很简单,我想限制 listView 的高度并在其中滚动以查看所有失败的设备并保持对话框的按钮不可触摸。
我怎样才能做到这一点?
我建议您使用约束布局来指示列表的最大高度,并将按钮附加到屏幕底部。
<?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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.