我通过膨胀以下XML创建了AlertDialog:
LayoutInflater li = LayoutInflater.from(this);
View dialogView = li.inflate(R.layout.activity_list_logs, null);
ListView list = (ListView) dialogView.findViewById(R.id.listDates);
显示alertDialog的完整代码是:
private void showLogs(final List<Absentees> abs) {
LayoutInflater li = LayoutInflater.from(this);
View dialogView = li.inflate(R.layout.activity_list_logs, null);
ListView list = (ListView) dialogView.findViewById(R.id.listDates);
list.setAdapter(new CustomAdapterTagAbsentees(this,abs));
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view,int position, long id) {
Absentees a =(Absentees)adapter.getItemAtPosition(position);
try
{
showLogDetails(a);
}
catch(Exception e)
{
show_popup(e+"");
}
}
});
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setView(dialogView);
alertDialogBuilder.setTitle("Attendance Details:");
alertDialogBuilder.setMessage("Day : ");
alertDialogBuilder
.setCancelable(true)
.setPositiveButton("Dismiss",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
使用的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
>
<ListView
android:id="@+id/listDates"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="20sp"
/>
</LinearLayout>
关于我的问题:
如果List足够小以适应屏幕,我可以看到“Dismiss”按钮(PositiveButton)。
否则如果List太大,我看不到“Dismiss”按钮
请帮助我!!,我试图在布局本身内部添加一个按钮,它显示正确,但我想知道为什么这种显示positiveButton的默认方式根本不显示
答案 0 :(得分:1)
使用RelativeLayout而不是Linear。将按钮设置为layout_alignParentBottom =&#34; true&#34;。将listview放到layout_above =&#34;按钮&#34;的id。这将强制按钮到屏幕底部,并在绘制列表视图之前为它们保留空间。否则列表视图会贪婪并且会占用所需的所有空间。
答案 1 :(得分:0)
这是我的问题的最终解决方案,感谢@blackbelt的建议
private void showLogs(final List<Absentees> abs) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
final ListAdapter adapter = new CustomAdapterTagAbsentees(this,abs);
alertDialogBuilder.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
showLogs(abs);
Absentees a =(Absentees) adapter.getItem(item);
showLogDetails(a);
}
});
alertDialogBuilder.setTitle("Attendance Details:");
//alertDialogBuilder.setMessage("Day : XXXX");
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("Dismiss",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
我必须发表评论/删除
alertDialogBuilder.setMessage("Day : XXXX ");
成功实施,不知道为什么!!