[英]Android: put 2 buttons in custom dialog to appear in center
我有一个自定义对话框。 我想将2个按钮水平放置在彼此附近,以显示在中间。 我该如何实现?
与“是/否”消息框非常相似。
我正在寻找一种在布局xml文件中执行此操作的方法。
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="horizontal"
android:width="wrap_content"
android:height="wrap_content"
android:layout_centerHorizontal="true">
... And two buttons here
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
您可能正在寻找与此类似的东西。 您可以设置“正向”和“负向”按钮,说出自己想要的内容,让每个人都做自己的事情。 在您的情况下(大多数情况下),在这里是和否是完美的!
也可以通过跟随该链接以及我之前使用的那个链接找到一些xml文件示例。
您可以随时使用xml定义Dialog内容的布局,然后使用layoutInflater设置为AlertDialog中的视图:
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog, (ViewGroup) findViewById(R.id.layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
文档来源: http : //developer.android.com/guide/topics/ui/dialogs.html#CustomDialog
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.