【问题标题】:AlertDialog not applying custom themeAlertDialog 未应用自定义主题
【发布时间】:2015-07-08 08:21:13
【问题描述】:

我想增加警告对话框消息文本的文本大小,所以我添加了以下样式:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/MainActionBar</item>

        <item name="android:textAppearance">@android:style/TextAppearance</item>
        <item name="android:textAppearanceLarge">@android:style/TextAppearance.Large</item>

        <item name="android:alertDialogStyle">@style/AlertDialog</item>
    </style>

    <style name="TextAppearance" parent="@android:style/TextAppearance">
        <item name="android:textSize">24sp</item> 
    </style>

    <style name="TextAppearance.Large">
        <item name="android:textSize">28sp</item>
    </style>

    <style name="AlertDialog" parent="@android:style/Theme.Holo.Light.Dialog">
        <item name="android:textAppearanceMedium">@style/TextAppearance.Large</item>
    </style>
</resources>

但是在显示对话框时没有应用样式。

我错过了什么?

【问题讨论】:

    标签: android android-alertdialog


    【解决方案1】:

    主题

     <style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
                <item name="android:background">@android:color/white</item>
                <item name="android:textColor">#000000</item>
            </style>
    

    警报对话框

        private void prompt() {
            AlertDialog.Builder alert = new AlertDialog.Builder(new ContextThemeWrapper(
                    getActivity(), R.style.AppCompatAlertDialogStyle));
            alert.setTitle("Update SSID");
            alert.setMessage(Html.fromHtml("<font color='#009688'>Do you want to update Heater SSID </font><font color='red'><u>" + Constants.WIFI_SSID.toUpperCase() + "</u></font><font color='#009688'> ?</font>"));
            LinearLayout layout = new LinearLayout(getActivity());
            layout.setOrientation(LinearLayout.VERTICAL);
            final TextView secure = new TextView(getActivity());
            secure.setTypeface(Util.setCustomFont(getActivity()));
            final EditText titleBox = new EditText(getActivity());
            titleBox.setTypeface(Util.setCustomFont(getActivity()));
            secure.setText(Html.fromHtml("<font color='#009688'><br><b>Enter new SSID</b></font>"));
                /*Edittext setting*/
            titleBox.setGravity(View.TEXT_ALIGNMENT_GRAVITY);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            params.setMargins(35, 5, 35, 10);
            titleBox.setLayoutParams(params);
            titleBox.setSingleLine(true);
            titleBox.setBackgroundResource(R.drawable.edit_text);
            titleBox.setTextColor(Color.parseColor("#505050"));
            titleBox.setInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
            // titleBox.setText("****************");
            titleBox.setTextSize(12);
           /*Textview setting*/
            LinearLayout.LayoutParams paramss = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            paramss.setMargins(35, 10, 35, 5);
            secure.setLayoutParams(paramss);
            layout.addView(secure);
            layout.addView(titleBox);
            alert.setView(layout);
            alert.setPositiveButton("CANCEL", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                }
            }).setNegativeButton("UPDATE", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                }
            });
            final AlertDialog alertDialog = alert.create();
            alertDialog.show();
            alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setEnabled(false);
            titleBox.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }
    
                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    int ssidLength = charSequence.length();
                    if (ssidLength == 0) {
                        return;
                    }
                    if (!Character.isLetterOrDigit(charSequence.charAt(ssidLength - 1))) {
                        titleBox.getText().delete(ssidLength - 1, ssidLength);
                        return;
                    }
                    if (ssidLength < 2) {
                        alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setEnabled(false);
                    } else {
                        alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setEnabled(true);
                    }
                    if (ssidLength > 12) {
                        titleBox.getText().delete(ssidLength - 1, ssidLength);
                    }
                }
    
                @Override
                public void afterTextChanged(Editable editable) {
    
                }
            });
    
        }
    

    edit_text.xml

    <?xml version="1.0" encoding="utf-8" ?>
    
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle"
        android:thickness="0dp">
        <stroke
            android:width="0.5dp"
            android:color="#009688" />
    
        <gradient
            android:angle="270"
            android:endColor="#ffffff"
            android:startColor="#ffffff"
            android:type="linear" />
    </shape>
    

    【讨论】:

    • 查看我的编辑。尽量避免在我的代码中进行不必要的自定义。如果需要,您可以在 Html.fromHtml 中指定字体大小..
    【解决方案2】:

    尝试将父级添加到“TextAppearance.Large”样式。

    <style name="TextAppearance.Large" parent="@android:style/TextAppearance">
            <item name="android:textSize">28sp</item>
    </style>
    

    【讨论】:

      猜你喜欢
      • 2020-08-12
      • 1970-01-01
      • 1970-01-01
      • 2016-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-28
      • 1970-01-01
      相关资源
      最近更新 更多