AlertDialog Button 文本格式在某些设备上完全错误

AlertDialog Button text formatting is completely wrong on some devices

我目前正在使用 AlertDialog 来提示用户并要求评分。当使用 AS 模拟器 (Nexus 5 API 23) 时,它的工作原理与我期望的完全一样。

来自模拟器:

但是,我在朋友的设备 LG Stylo(Android 5.1.1,Api 22)上测试了对话框,格式完全错误。按钮的顺序错误并且格式根本不正确。

来自 LG Stylo:

我不确定如何确保 AlertDialog 的格式正确,老实说我不知道​​为什么 LG Stylo 的格式不正确,很遗憾我没有目前要测试它的另一台设备。

提前致谢!

AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setMessage(R.string.rateMeText)
                .setPositiveButton(R.string.rateMe, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        editor.putBoolean(KEY_REMIND_ME_LATER, false);
                        editor.commit();
                        try {
                            intent.setData(Uri.parse(myContext.getString(R.string.playStoreMarketLink) + APP_PNAME));
                            myContext.startActivity(intent);
                        }catch (Exception e){
                            intent.setData(Uri.parse(myContext.getString(R.string.playStoreHttpLink) + APP_PNAME));
                            myContext.startActivity(intent);

                        }
                        dialog.dismiss();
                    }
                })
                .setNegativeButton(R.string.noThanksAndDoNotAskAgain, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (editor != null) {
                            editor.putBoolean(KEY_REMIND_ME_LATER, false);
                            editor.commit();
                            /*editor.putBoolean(KEY_DONT_SHOW_AGAIN, true);*/
                            editor.commit();
                        }
                        dialog.dismiss();
                    }
                })
                .setNeutralButton(R.string.remindMeLater, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        editor.putLong(KEY_LAUNCH_COUNT_PRESSED_REMIND, launches);
                        editor.putBoolean(KEY_REMIND_ME_LATER, true);
                        editor.commit();
                        System.out.print(sharedPrefs.getLong(KEY_LAUNCH_COUNT_PRESSED_REMIND, 27727));
                        dialog.dismiss();
                    }
                });
        builder.create().show();

您必须使用现有样式作为父样式创建样式,然后在 AlertDialog.Builder() 方法中使用它。

看起来像这样:

<style name="myAlertStyle" parent="AlertDialog.Material">
    <item name="fullDark">@empty</item>
    <item name="topDark">@empty</item>
    <item name="centerDark">@empty</item>
    <item name="bottomDark">@empty</item>
    <item name="fullBright">@empty</item>
    <item name="topBright">@empty</item>
    <item name="centerBright">@empty</item>
    <item name="bottomBright">@empty</item>
    <item name="bottomMedium">@empty</item>
    <item name="centerMedium">@empty</item>
    <item name="layout">@layout/alert_dialog_material</item>
    <item name="listLayout">@layout/select_dialog_material</item>
    <item name="progressLayout">@layout/progress_dialog_material</item>
    <item name="horizontalProgressLayout">@layout/alert_dialog_progress_material</item>
    <item name="listItemLayout">@layout/select_dialog_item_material</item>
    <item name="multiChoiceItemLayout">@layout/select_dialog_multichoice_material</item>
    <item name="singleChoiceItemLayout">@layout/select_dialog_singlechoice_material</item>
</style>

然后您只需像这样使用 AlertDialog.Builder():

AlertDialog.Builder(mContext, R.style.myAlertStyle);

值得注意的是,这几乎是 api-22 支持库中的一个错误,因为它试图将所有按钮放在一行中。它已在 api-23 支持库中修复,方法是切换到每个按钮都在其自己的行上的新布局。

我在装有 KitKat 4.4.4 的设备上,在两个不同的项目中使用支持库 AlertDialog 测试了您的代码。

两个测试的相同代码:

import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("If you like this app, please rate it on the Google Play Store!")
                .setPositiveButton("Rate me!", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .setNegativeButton("No thanks and do not ask me again", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .setNeutralButton("Remind me later", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
        builder.create().show();
    }
}

首先,在具有此 gradle 配置的项目中(所有 api-22 设置):

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.1'
}

这是结果:

然后,在具有此配置的不同项目中(所有 api-23 设置):

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.example.testprojecttwo"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
}

这是相同代码的结果:

因此,解决此问题的一种方法是使用 api-23 支持库。