Java 安卓:在自定义对话框中放置两个按钮以显示在中间

Java 安卓:在自定义对话框中放置两个按钮以显示在中间,java,android,Java,Android,我有一个自定义对话框。 我想把两个按钮,一个靠近另一个水平,出现在中心。 我怎样才能做到这一点 与“是/否”消息框非常相似 我正在寻找一种在布局xml文件中执行此操作的方法。来自 你可能正在寻找类似的东西。你可以设置“肯定”和“否定”按钮来说出你想说的任何话,并让每个人都做自己的事情。在你和大多数情况下,是和否在这里都是完美的 通过遵循该链接以及我以前使用的这个链接,还可以找到一些xml文件的示例 从 你可能正在寻找类似的东西。你可以设置“肯定”和“否定”按钮来说出你想说的任何话,并让每个人都做

我有一个自定义对话框。 我想把两个按钮,一个靠近另一个水平,出现在中心。 我怎样才能做到这一点

与“是/否”消息框非常相似

我正在寻找一种在布局xml文件中执行此操作的方法。

来自

你可能正在寻找类似的东西。你可以设置“肯定”和“否定”按钮来说出你想说的任何话,并让每个人都做自己的事情。在你和大多数情况下,是和否在这里都是完美的

通过遵循该链接以及我以前使用的这个链接,还可以找到一些xml文件的示例

你可能正在寻找类似的东西。你可以设置“肯定”和“否定”按钮来说出你想说的任何话,并让每个人都做自己的事情。在你和大多数情况下,是和否在这里都是完美的

通过遵循该链接以及我以前使用的这个链接,还可以找到一些xml文件的示例


您可以使用xml定义对话框内容的布局,然后使用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();

文档来源:

您可以使用xml定义对话框内容的布局,然后使用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();

文档来源:

谢谢您的回答。但我指的只是xml布局配置。谢谢你的回答。但我指的只是xml布局配置,最终对我有用的不是android:gracity=center,而是android:layout\u centerHorizontal=true。相应地编辑答案。还有一点输入错误:android:width和android:height,而不是android:layout\u width和android:layout\u height。也解决了这个问题,你是对的。我不确定,并且忘记了另一个参数,我家里没有Eclipse,无法检查它。很抱歉更正。最终对我有用的不是android:gracity=center,而是我使用的android:layout\u centerHorizontal=true。相应地编辑答案。还有一点输入错误:android:width和android:height,而不是android:layout\u width和android:layout\u height。也解决了这个问题,你是对的。我不确定,并且忘记了另一个参数,我家里没有Eclipse,无法检查它。很抱歉更正。谢谢,但我想知道如何在布局xml文件中执行此操作。@goryachex,不用担心,但第二个链接中有一些示例:。谢谢,但我想知道如何在布局xml文件中执行此操作。@goryachex,不用担心,但第二个链接中有一些示例:。
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();