希望得到您的帮助,非常感谢。提前致谢。
编辑: 请查看我在12月3日发布的答案,但那并不是一个解决方案。
希望得到您的帮助,非常感谢。提前致谢。
编辑: 请查看我在12月3日发布的答案,但那并不是一个解决方案。
可以通过使用堆叠按钮而不是行按钮来解决这个问题。以下是我的解决方法,它使用了AppCompat库:
代码 import android.support.v7.app.AlertDialog;
AlertDialog.Builder builder;
builder = new AlertDialog.Builder(context, R.style.StackedAlertDialogStyle);
builder.setTitle("Title");
builder.setMessage("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc dignissim purus eget gravida mollis. Integer in auctor turpis. Morbi auctor, diam eget vestibulum congue, quam arcu pulvinar dui, blandit egestas erat enim non ligula." +
" Nunc quis laoreet libero. Aliquam consectetur nibh eu arcu eleifend efficitur.");
builder.setPositiveButton("Positive Button", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNeutralButton("Neutral Button", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("Cancel Button", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
try{
final Button button = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
LinearLayout linearLayout = (LinearLayout) button.getParent();
linearLayout.setOrientation(LinearLayout.VERTICAL);
} catch(Exception ex){
//ignore it
}
风格
<style name="StackedAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="buttonBarButtonStyle">@style/StackedButtonBarButtonStyle</item>
</style>
<style name="StackedButtonBarButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:layout_gravity">right</item>
</style>
结果
跟进 - 由于我的声望较低,无法发布超过两个链接,因此我必须回答我的问题,而不是编辑它。
以下是我尝试使用buttonBarStyle和buttonBarButtonStyle样式来实现任何改进的方式 - 在这里查看结果:
不幸的是,这些显然不是理想的解决方案。
<resources>
<style name="AppBaseTheme" parent="android:Theme.Material.Light">
<!-- AlertDialog Style override in order to try to fix non line breaking buttons -->
<item name="android:alertDialogTheme">@style/CustomAlertDialogStyle</item>
</style>
<style name="CustomAlertDialogStyle" parent="android:Theme.Material.Light.Dialog.Alert">
<item name="android:buttonBarButtonStyle">@style/CustomButtonBarButtonStyle</item>
<item name="android:buttonBarStyle">@style/CustomButtonBarStyle</item>
</style>
<style name="CustomButtonBarStyle" parent="@android:style/Widget.Material.Light.ButtonBar.AlertDialog">
<!-- Making sure, the button bar uses parent width and is not restricted in height -->
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:height">@null</item>
<item name="android:minHeight">@null</item>
</style>
<style name="CustomButtonBarButtonStyle" parent="@android:style/Widget.Material.Light.Button.Borderless.Colored">
<!-- Setting the weight as follows should result in equally wide buttons filling the alert dialog width,
but instead they span further out of the dialog, breaking in multiple lines though -->
<item name="android:layout_width">0dp</item>
<item name="android:layout_weight">1</item>
<!-- setting a fixed width as follows results in narrow buttons with line breaks, but of course this is not a solution -->
<!-- <item name="android:width">100dp</item> -->
</style>
</resources>
21
的操作系统版本进行定位,那么可以从哪些父样式中继承呢?当使用类似于"@android:style/Widget.Material.Light.Button.Borderless.Colored"
作为父样式时,我会收到一个错误提示,指出该样式仅在API 21
中可用。 - Sakiboy总结一下这个话题,供有兴趣的人参考:
Android Material主题在警告对话框中自动调整按钮宽度上似乎存在一个bug。
例如,当你有三个按钮,其中一个按钮有多个单词时,积极的按钮很可能会挤出对话框到右边,而不是将有多个单词的按钮分成多行,以使所有按钮都适应按钮栏,就像基本主题/ Holo主题所做的那样。
似乎没有仅通过应用buttonBarStyle和/或buttonBarButtonStyle的更改来解决问题,因为它们的样式默认情况下不限制按钮文本被包装成多行。
当然,在一般情况下,Material主题对话框按钮需要比其他主题更多的空间,由于字母大写、加粗和慷慨的填充和背景,但这不是问题的根源,它只是让它看起来比如果样式需要更少的空间就更快了。
唯一解决此问题的方法似乎是缩短按钮标题,如果您不想从Material的外观和感觉中脱颖而出(例如使按钮文本大小变小和/或将allCaps设置为false)
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
try {
LinearLayout linearLayout = (LinearLayout) alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).getParent();
if (linearLayout != null) {
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setGravity(Gravity.RIGHT);
}
} catch (Exception ignored) {
}
}
});
我想到了一个解决方法,借助于 Andrey T 的答案 (https://dev59.com/Il4d5IYBdhLWcg3wFPQ7#29662638):
首先,您需要创建一个实用方法,仅在需要时才包装按钮:
public static void applyWorkaroundForButtonWidthsTooWide(Button dialogButton) {
if (dialogButton == null)
return;
if (!(dialogButton.getParent() instanceof LinearLayout))
return;
// Workaround for buttons too large in alternate languages.
final LinearLayout linearLayout = (LinearLayout) dialogButton.getParent();
linearLayout.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop,
int oldRight, int oldBottom) {
if (right - left > 0) {
final int parentWidth = linearLayout.getWidth();
int childrenWidth = 0;
for (int i = 0; i < linearLayout.getChildCount(); ++i)
childrenWidth += linearLayout.getChildAt(i).getWidth();
if (childrenWidth > parentWidth) {
// Apply stacked buttons
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setPadding(linearLayout.getPaddingLeft(), 0, linearLayout.getPaddingRight(),
linearLayout.getPaddingBottom());
for (int i = 0; i < linearLayout.getChildCount(); ++i) {
if (linearLayout.getChildAt(i) instanceof Button) {
final Button child = (Button) linearLayout.getChildAt(i);
child.setGravity(Gravity.END | Gravity.CENTER_VERTICAL);
final LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) child.getLayoutParams();
params.width = LinearLayout.LayoutParams.MATCH_PARENT;
params.gravity = Gravity.END;
child.setLayoutParams(params);
} else if (linearLayout.getChildAt(i) instanceof Space) {
linearLayout.removeViewAt(i--);
}
}
}
linearLayout.removeOnLayoutChangeListener(this);
}
}
});
}
现在,在显示对话框时调用此实用方法:
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
MaterialAlertDialogUtils.applyWorkaroundForButtonWidthsTooWide(dialog.getButton(AlertDialog.BUTTON_POSITIVE));
}
});