自定义警报对话框获取不需要的填充

时间:2011-03-16 17:30:35

标签: android alertdialog

我尝试制作自定义提醒对话框,但效果非常好。功能完美,但视图表现得很奇怪。对话框布局由按钮和列表视图组成。正如您将在下面的屏幕截图中看到的那样,按钮在顶部和底部都有一个边距。

我没有看到这些利润的任何原因,我将非常感谢一些帮助:)

由于某些原因我无法发布我的xml布局,但我可以确定它不包含任何类型的填充或边距

Java代码:

View dialogView = ((LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.choose_catagory_dialog_layout, null, false);
Button footerButton = (Button) dialogView.findViewById(R.id.choose_catagory_dialog_footer_button);
footerButton.setOnClickListener(ButtonClickEvent);

builder = new AlertDialog.Builder(mContext);
builder.setView(dialogView);
builder.setTitle(R.string.choose_catagory);

builder.setAdapter(spinnerAdapter, ListclickEvent);
alert = builder.create();
alert.getListView().setVerticalFadingEdgeEnabled(false);
alert.setOwnerActivity((Activity) mContext);

截图:

http://cl.ly/3S2y3p3E0e3H2o1I272m

3 个答案:

答案 0 :(得分:19)

AlertDialog的CustomPanel有一个5dp的顶部和底部填充。您可以使用以下方法覆盖这些:

alert.setView(dialogView, 0, 0, 0, 0);

答案 1 :(得分:10)

对我来说,似乎builder.setView(dialogView, 0, 0, 0, 0);设置为@hide并且不可用。最后,我必须在调用show()

后设置自定义视图父级的填充

例如:

// Inflate custom dialog view
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
CustomDialogView dialogView = (CustomDialogView)inflater.inflate(R.layout.dialog_view, null);

// Create and show custom alert dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.dialog_title);
builder.setView(dialogView);
builder.setPositiveButton(R.string.dialog_ok, this);
builder.setNegativeButton(R.string.dialog_cancel, this);
builder.show();

// Remove padding from parent
ViewGroup parent = (ViewGroup)dialogView.getParent();
parent.setPadding(0, 0, 0, 0);

答案 2 :(得分:2)

以下代码可让您完全控制对话框视图:

final Dialog dlg = new Dialog(this);
View action_layout = ((LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.contacts_action, null);
dlg.requestWindowFeature(Window.FEATURE_NO_TITLE);  
Window window = dlg.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.FILL_HORIZONTAL;
window.setAttributes(wlp);
dlg.setCancelable(true);
dlg.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dlg.setContentView(action_layout);
dlg.show();