Android 如何确定AlertDialog按钮的最大文本长度?

Android 如何确定AlertDialog按钮的最大文本长度?,android,dialog,font-size,android-alertdialog,Android,Dialog,Font Size,Android Alertdialog,我遇到了一个非常非常简单的AlertDialog问题,它创建了我的关于对话框,有三个选择。看看它在模拟器、我的HTC Sensation和Galaxy S2上的外观: 该对话框由以下(伪)代码创建: 它是如此简单,我从来没有想过最大文本长度在不同的设备上有如此大的差异!另外:我认为三星对“免责声明”的撕碎是如此的混乱,更不用说“市场”这个词的缺失了 当这已经是一个问题时,我应该如何创建一个简单的AlertDialog? 有什么线索吗?想一想一个设备,它设置了如此大的字体,甚至“取消”都不再适合

我遇到了一个非常非常简单的
AlertDialog
问题,它创建了我的关于对话框,有三个选择。看看它在模拟器、我的HTC Sensation和Galaxy S2上的外观:

该对话框由以下(伪)代码创建:

它是如此简单,我从来没有想过最大文本长度在不同的设备上有如此大的差异!另外:我认为三星对“免责声明”的撕碎是如此的混乱,更不用说“市场”这个词的缺失了

当这已经是一个问题时,我应该如何创建一个简单的
AlertDialog
? 有什么线索吗?想一想一个设备,它设置了如此大的字体,甚至“取消”都不再适合了!唉,我怎么能阻止这样的事情呢


谢谢你的建议

以“DP”(与密度无关的像素)为单位指定字体大小。这将指示每部手机缩放字体,使其在所有设备上看起来(或多或少)相同

我不知道为什么这个答案被否决了,我只能假设你不知道怎么做我的建议。让我提供更多细节

builder.setPositiveButton("OK", null);
builder.setNeutralButton("Disclaimer", [...]);
builder.setNegativeButton("Jetzt zum Market", [...]);

AlertDialog myDialog = builder.create();

/* You'll have to play with this value to see what looks right */
float textSize = 15.0f;

Button positive = myDialog.getButton(BUTTON_POSITIVE);
positive.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize);

Button neutral = myDialog.getButton(BUTTON_NEUTRAL);
neutral.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize);

Button negative = myDialog.getButton(BUTTON_NEGATIVE);
negative.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize);

myDialog.show();
这将使按钮的字体大小与对话框的标题和正文不同。您可以使用以下方法替换AlertDialog的内容视图:

在为内容创建的布局中,设置字体大小以匹配上面“textSize”变量的最终结果。您可以在布局中直接使用“dp”单位


如果您还有其他问题,请让我知道它们是什么,不要只是投反对票。

德鲁·德纳多的回答几乎是正确的

myDialog.getButton(BUTTON_正值)返回null,因为它尚未创建

您需要覆盖onShow:

final AlertDialog alert= builder.create();

alert.setOnShowListener(new DialogInterface.OnShowListener() {

    @Override
    public void onShow(DialogInterface dialog) {

        float textSize = 15.0f;

        Button positive = alert.getButton(AlertDialog.BUTTON_POSITIVE);
        positive.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize);

        Button neutral = alert.getButton(AlertDialog.BUTTON_NEUTRAL);
        neutral.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize);

        Button negative = alert.getButton(AlertDialog.BUTTON_NEGATIVE);
        negative.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize);
    }
});

好吧,我试着用你的方法得到对话框按钮:但总是得到空值!另外:即使它有效,我如何确定正确的尺寸,使所有东西都适合?我不能像你建议的那样“玩”,因为我并不拥有所有可用的Android设备——因此,如果它起作用的话,这不是正确的方式-(但我想我必须走定制的道路——只是为了让按钮合适……你能自己试试吗?我从来没有用getButton()方法交付过任何按钮!总是空的。最好使用TypedValue.COMPLEX\u UNIT\u SP。)
final AlertDialog alert= builder.create();

alert.setOnShowListener(new DialogInterface.OnShowListener() {

    @Override
    public void onShow(DialogInterface dialog) {

        float textSize = 15.0f;

        Button positive = alert.getButton(AlertDialog.BUTTON_POSITIVE);
        positive.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize);

        Button neutral = alert.getButton(AlertDialog.BUTTON_NEUTRAL);
        neutral.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize);

        Button negative = alert.getButton(AlertDialog.BUTTON_NEGATIVE);
        negative.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize);
    }
});