根视图上的边距无效。如其他答案所述,尝试向父布局添加填充。
但是,我建议您不要用java创建对话框布局,而是膨胀xml并使用
AppCompatEditText
如果要使用线条背景
这是给你的示例代码
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Message");
// Why are you setting message here when you are inflating custom view?
// You need to add another TextView in xml if you want to set message here
// Otherwise the message will not be shown
// builder.setMessage("Do you want to\n"+""+"exit from app");
View view = LayoutInflater.from(this).inflate(R.layout.dialog_layout, null);
final AppCompatEditText input = (AppCompatEditText) view.findViewById(R.id.editText);
builder.setView(view);
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "You exit from app " + input.getText().toString(),
Toast.LENGTH_LONG).show();
}
});
AlertDialog alert = builder.create();
alert.show();
最后,您无法在创建对话框后立即获得按钮。你需要在
OnShowListener
如果您想自定义按钮文本颜色。或使用
android.support.v7.app.AlertDialog
用于较新的Dialog设计。
Button nbutton = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
// will be null
// nbutton.setTextColor(Color.parseColor("#7e7e7e"));
Button pbutton = alert.getButton(DialogInterface.BUTTON_POSITIVE);
// will be null
// pbutton.setTextColor(Color.parseColor("#109c8f"));
对话框布局.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical">
<android.support.v7.widget.AppCompatEditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="16dp"
app:backgroundTint="@color/colorPrimary"/>
</LinearLayout>