13

AlertDialog以下のコードでを作成します。何らかの理由で、ハニカム以上で追加の背景 (写真を参照) を取得しています。コードは、ハニカムよりも下であれば問題 なくクラッシュします。MyCustomDialogは単にTheme.DialogAPI-11 未満およびTheme.Holo.DialogAPI-11 以降を対象としています。

  1. 余分な背景を取得している理由はわかりますか?
  2. API < 11 でクラッシュする理由はありますか? テーマを削除すると正常に動作します。

更新により、質問 2 の答えがわかりました。コンストラクターAlertDialog.Builder(Context context, int theme)は API 11 で導入されたようです。私の修正は、単に行を次のように変更することでした。

final AlertDialog.Builder builder = (Integer.parseInt(android.os.Build.VERSION.SDK) < 11)? new AlertDialog.Builder(this) : new AlertDialog.Builder(this,R.style.JumpDialog);

質問 1 についてまだサポートが必要です

ここに画像の説明を入力

private Dialog setupKeyBoardDialog() {
    if (mContact.getLocaleId() != -1) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this,R.style.MyCustomDialog);
        builder.setTitle("Keyboards");

        mKeyboardLayouts = new KeyboardLayoutGroup();
        mKeyboardLayouts.layoutNames = new CharSequence[(int) jni.getNumKeyLayouts()];
        mKeyboardLayouts.layoutValue = new ArrayList<Integer>();

        for (int i = 0; i < jni.getNumKeyLayouts(); i++) {
            mKeyboardLayouts.layoutNames[i] = jni.LayoutInfoForIndex(i).getName();
            mKeyboardLayouts.layoutValue.add(i, (int) jni.LayoutInfoForIndex(i).getLocale_id());
        }

        final int selectedItem = mKeyboardLayouts.layoutValue.indexOf(mContact.getLocaleId());

        builder.setSingleChoiceItems(mKeyboardLayouts.layoutNames, selectedItem, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int item) {
                mContact.setLocaleId(mKeyboardLayouts.layoutValue.get(item));
                mContactsDB.saveContact(mContact, true);

                dialog.dismiss();
                initializeSettingsList();
            }
        });

        final AlertDialog dialog = builder.create();
        dialog.setButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogBox, int arg1) {
                dialogBox.cancel();
            }
        });

        return dialog;
    }

    return null;
}
4

3 に答える 3

17

答えを見つけた

  1. AlertDialog には、AlertDialog クラスの各テーマの静的定数があり、標準のテーマは使用しません。私が置き換えたとき、R.style.MyThemeまたはコードandroid.R.style.Theme_Holo_DialogAlertDialog.THEME_HOLO_LIGHTうまくいきました。
  2. コンストラクターAlertDialog.Builder(Context context, int theme)は API 11 で導入されたようです。私の修正は、単に行を次のように変更することでした。

    final AlertDialog.Builder builder;
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        builder = new AlertDialog.Builder(this);
    } else {
        builder = new AlertDialog.Builder(this,R.style.JumpDialog);
    }
    
于 2011-12-31T08:26:40.193 に答える
6

new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.JumpDialog))代わりに使用してみることができますnew AlertDialog.Builder(this, R.style.JumpDialog)

于 2013-08-21T16:32:35.623 に答える
2

(受け入れられた解決策のように)デフォルトのものに固執することなくダイアログテーマをカスタマイズする方法を探している人にとって、ロリポップから始めて、余分な背景が最終的に削除されたようです. したがって、デフォルトのものから継承するテーマを作成できるようになりました (AppCompat の例):

<!-- default theme for L devices -->
<style name="SelectionDialog" parent="Theme.AppCompat.Light.Dialog">
    <item name="android:textColor">@color/default_text_color_holo_light</item>
</style>
<!-- theme for Pre-L devices -->
<style name="SelectionDialog.PreL">
    <!-- remove the dialog window background -->
    <item name="android:windowBackground">@color/transparent</item>
</style>

そして、次のコードでビルダーをインスタンス化します。

AlertDialog.Builder builder = new AlertDialog.Builder(
            getActivity(),                
            Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT ?
                    R.style.SelectionDialog :
                    R.style.SelectionDialog_PreL);

もちろん、これはリソース フォルダー (values/およびvalues-v21/) でも実行できます。

于 2015-02-17T12:41:40.490 に答える