【问题标题】:How to determine maximum text length for AlertDialog buttons?如何确定 AlertDialog 按钮的最大文本长度?
【发布时间】:2011-12-28 03:20:47
【问题描述】:

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

对话框是由这个(伪)代码创建的:

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

这很简单,我从没想过不同设备上的最大文本长度差异如此之大!另外:我认为三星撕毁“免责声明”是一团糟,更不用说缺少“市场”这个词了。

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

多谢指教!!

【问题讨论】:

    标签: android dialog font-size android-alertdialog


    【解决方案1】:

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

    http://developer.android.com/guide/practices/screens_support.html

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

    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 的内容视图:

    http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog

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

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

    【讨论】:

    • 好吧,我试着用你的方法来获取对话框按钮:但总是得到空!另外:即使它有效,我如何确定正确的尺寸以使一切都适合?我不能像你建议的那样“玩”,因为我不拥有所有可用的 Android 设备 - 所以如果它有效,这不是正确的方法。 :-( 但我想我将不得不采用自定义方式 - 只是为了让按钮适合......
    • 你能自己试试吗?我从来没有通过 getButton() 方法获得任何按钮!总是为空。
    【解决方案2】:

    Drew DeNardo 的回答几乎是正确的。

    myDialog.getButton(BUTTON_POSITIVE) 正在返回 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);
        }
    });
    

    【讨论】:

    • 最好使用TypedValue.COMPLEX_UNIT_SP
    猜你喜欢
    • 2020-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-30
    • 1970-01-01
    相关资源
    最近更新 更多