gpt4 book ai didi

android - 如何在警报对话框中将中性按钮位置设置为居中而不是左侧

转载 作者:太空宇宙 更新时间:2023-11-03 12:47:11 25 4
gpt4 key购买 nike

下面是我的代码,它在左侧显示ok 按钮

 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("No product found");
builder.setCancelable(false);
builder.setNeutralButton(
"Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
((MainActivity)getActivity()).fragment_Scan_call();
}
});
// Must call show() prior to fetching text view
AlertDialog alert = builder.create();
alert.show();

最佳答案

经过多次 Google 搜索和研究默认 AlertDialog 布局后,我发现在 neutral button-ve/+ve buttons 之间有一个空间布局buttonBarLayout 中按钮所在的位置“1”。

因此,首先,我们需要删除该空间或使其可见性GONE(invisible 将让它在 buttonBarLayout) 此外,我们最好使用 onShowListner 方法,而不是在通过以下方式显示对话框后这样做:

 alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Button neutralButton = alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL);
LinearLayout view = (LinearLayout) neutralButtonOrAnyOtherBtnFromThe3Btns.getParent();
Space space= (Space) view.getChildAt(1);
space.setVisibility(View.GONE);
}
});

然后你可以根据需要设计按钮,或者如果你只想在中性按钮之前获得空间,你可以使用 this question

关于android - 如何在警报对话框中将中性按钮位置设置为居中而不是左侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40778195/

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