在构建AlertDialog时,您需要禁用内置的顶部和底部Padding。
具体来说,设置AlertDialog.Builder的setView方法中提供的布局的顶部和底部边距为零。
例如,在以下代码片段中,使用0dp的LinearLayout作为AlertDialog的主视图,LinearLayout的顶部和底部填充均为零:
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setPadding(0, 0, 0, 0);
// add views to the linear layout
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(linearLayout)
.setTitle("Example dialog")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// handle OK button
}
});
AlertDialog dialog = builder.create();
dialog.show();