gpt4 book ai didi

android - 如何自定义 alertdialog 以便按钮适合 alertdialog

转载 作者:行者123 更新时间:2023-11-29 01:24:57 26 4
gpt4 key购买 nike

我正在使用以下代码来创建警报对话框。

AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, android.R.style.Theme_Holo_Light));
View pickerView = getLayoutInflater().inflate(R.layout.picker_dialog, null);
builder.setView(pickerView);
builder.setMessage("AlertDialog").setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "got it!", Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).setNeutralButton("Neutral", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "neutralize", Toast.LENGTH_SHORT).show();
}
});

AlertDialog alert = builder.create();
alert.setTitle("number picker");
alert.show();

问题是当我使用这段代码创建对话框时,这三个按钮没有均匀放置,如下所示:

enter image description here

其实我想得到的是这样的:

enter image description here

这两个按钮是等量放置的

我知道这是 alertdialog 主题的问题。但我尝试了无数次我无法改变任何东西。

谁能告诉我如何处理主题以获得像第二个那样的警告对话框?

picker_dialog 的布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="input"/>

</LinearLayout>

我听从 Android - Make AlertDIalog buttons a uniform size 的建议.代码如下:

AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, android.R.style.Theme_Dialog));
View pickerView = getLayoutInflater().inflate(R.layout.picker_dialog, null);
builder.setView(pickerView);
builder.setMessage("AlertDialog").setCancelable(false).setPositiveButton("Verify", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "got it!", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Neutral", Toast.LENGTH_SHORT).show();
}
});

final AlertDialog alert = builder.create();
alert.setTitle("number picker");
alert.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Button posButton = alert.getButton(DialogInterface.BUTTON_POSITIVE);
Button negButton = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
Button neuButton = alert.getButton(DialogInterface.BUTTON_NEUTRAL);

LinearLayout.LayoutParams posParams = (LinearLayout.LayoutParams) posButton.getLayoutParams();
posParams.weight = 1;
posParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
posParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;

LinearLayout.LayoutParams negParams = (LinearLayout.LayoutParams) negButton.getLayoutParams();
negParams.weight = 1;
negParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
posParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
LinearLayout.LayoutParams neuParams = (LinearLayout.LayoutParams) neuButton.getLayoutParams();
neuParams.weight = 1;
neuParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
neuParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;


posButton.setLayoutParams(posParams);
negButton.setLayoutParams(negParams);
neuButton.setLayoutParams(neuParams);
}
});
alert.show();

以上是按照上面链接中的建议完成的代码。我只得到这个:enter image description here

似乎正按钮已推到右角并消失了。

谁能解决这个问题?

根据Kushan的建议,我将layout设置代码从dialog onshow listener中取出来,完整代码如下:

AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, android.R.style.Theme_Dialog));
View pickerView = getLayoutInflater().inflate(R.layout.picker_dialog, null);
builder.setView(pickerView);
builder.setMessage("AlertDialog").setCancelable(false).setPositiveButton("Positive", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "got it!", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("Negative", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setNeutralButton("Neutral", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Neutral", Toast.LENGTH_SHORT).show();
}
});

AlertDialog alert = builder.create();
alert.setTitle("number picker");
alert.show();

LinearLayout.LayoutParams buttonParams;
Button buttonPositive = alert.getButton(AlertDialog.BUTTON_POSITIVE);
buttonParams = (LinearLayout.LayoutParams) buttonPositive.getLayoutParams();
buttonParams.weight = 1;
buttonParams.width = buttonParams.MATCH_PARENT;

Button buttonNegative = alert.getButton(AlertDialog.BUTTON_NEGATIVE);
buttonParams = (LinearLayout.LayoutParams) buttonNegative.getLayoutParams();
buttonParams.weight = 1;
buttonParams.width = buttonParams.MATCH_PARENT;

Button buttonNeutral = alert.getButton(AlertDialog.BUTTON_NEUTRAL);
buttonParams = (LinearLayout.LayoutParams) buttonNeutral.getLayoutParams();
buttonParams.weight = 1;
buttonParams.width = buttonParams.MATCH_PARENT;

我得到了和上面一样的结果

enter image description here

最佳答案

尝试像下面这样的警告对话框代码:

 AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, android.R.style.Theme_Holo_Light));
View pickerView = getLayoutInflater().inflate(R.layout.picker_dialog, null);
builder.setView(pickerView);
builder.setMessage("AlertDialog").setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "got it!", Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).setNeutralButton("Neutral", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "neutralize", Toast.LENGTH_SHORT).show();
}
});

AlertDialog alert = builder.create();
alert.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Button negativeButton = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_NEGATIVE);
Button positiveButton = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 2f);
negativeButton.setLayoutParams(params);
positiveButton.setLayoutParams(params);

negativeButton.invalidate();
positiveButton.invalidate();
}
});
alert.setTitle("number picker");
alert.show();

关于android - 如何自定义 alertdialog 以便按钮适合 alertdialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34555107/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com