How to change theme for AlertDialog

前端 未结 15 1896
名媛妹妹
名媛妹妹 2020-11-22 09:14

I was wondering if someone could help me out. I am trying to create a custom AlertDialog. In order to do this, I added the following line of code in styles.xml



        
相关标签:
15条回答
  • 2020-11-22 09:27
     <style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
        <!-- Used for the buttons -->
        <item name="colorAccent">@color/colorAccent</item>
        <!-- Used for the title and text -->
        <item name="android:textColorPrimary">#FFFFFF</item>
        <!-- Used for the background -->
        <item name="android:background">@color/teal</item>
    </style>
    
    
    
    
    
    new AlertDialog.Builder(new ContextThemeWrapper(context,R.style.AlertDialogCustom))
                .setMessage(Html.fromHtml(Msg))
                .setPositiveButton(posBtn, okListener)
                .setNegativeButton(negBtn, null)
                .create()
                .show();
    
    0 讨论(0)
  • 2020-11-22 09:27

    Anyone trying to do this within a Fragment (using the support library i.e. pre API 11) should go with this:

    public class LoadingDialogFragment extends DialogFragment {
        public static final String ID = "loadingDialog";
    
        public static LoadingDialogFragment newInstance() {
            LoadingDialogFragment f = new LoadingDialogFragment();
    
            return f;
        }
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            StyleAlertDialog adb = new StyleAlertDialog(getActivity(), R.style.Your_Style);
            adb.setView(getActivity().getLayoutInflater().inflate(R.layout.fragment_dialog_layout, null));
            return adb;
        }
    
        private class StyleAlertDialog extends AlertDialog {
            protected StyleAlertDialog(Context context, int theme) {
                super(context, theme);
            }
        }
    }
    

    @Rflexor gave me the nudge to extend AlertDialog and expose the constructor thanks

    0 讨论(0)
  • 2020-11-22 09:30

    Arve Waltin's solution looks good, although I haven't tested it yet. There is another solution in case you have trouble getting that to work.... Extend AlertDialog.Builder and override all the methods (eg. setText, setTitle, setView, etc) to not set the actual Dialog's text/title/view, but to create a new view within the Dialog's View do everything in there. Then you are free to style everything as you please.

    To clarify, as far as the parent class is concerned, the View is set, and nothing else.

    As far as your custom extended class is concerned, everything is done within that view.

    0 讨论(0)
  • 2020-11-22 09:30

    You can override the default theme used by DialogFragments spawned by an activity by modifying the activity's theme's attributes....

    set the activity's theme in AndroidManifest.xml.

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.helloworld">
    
        <application
            android:name=".App"
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">     <!-- set all Activity themes to your custom theme -->
    
            .....
    
        </application>
    
    </manifest>
    

    in the values/styles.xml, override the item used to determine what theme to use for spawned DialogFragments

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <style name="AppTheme" parent="Theme.AppCompat">
    
            <!-- override the default theme for DialogFragments -->
            <item name="android:dialogTheme">@style/AppTheme.Dialog</item>
    
        </style>
    
        .....
    
    </resources>
    

    in the values/styles.xml, define and configure the theme you want to use for DialogFragments

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        .....
    
        <!--
            configure your custom theme for DialogFragments...
        -->
        <style name="AppTheme.Dialog" parent="Theme.AppCompat.Dialog.MinWidth">
    
            <!-- override the default theme for DialogFragments spawned by this DialogFragment -->
            <item name="android:dialogTheme">@style/AppTheme.Dialog</item>
    
            <!--
                OPTIONAL: override the background for the dialog...i am using a dark theme,
                and for some reason, there is no themes for dialogs with dark backgrounds,
                so, i made my own.
            -->
            <item name="android:windowBackground">@drawable/dialog__window_background</item>
    
            <!--
                add the title to the dialog's theme. you can remove it later by using
                DialogFragment.setStyle()
            -->
            <item name="android:windowNoTitle">false</item>
            <item name="windowNoTitle">?android:windowNoTitle</item>
    
        </style>
    
        .....
    
    </resources>
    

    OPTIONAL: if you use a dark theme, and overrode android:windowBackground like i did in AppTheme.Dialog, then add a drawable/dialog__window_background.xml file with the contents:

    <?xml version="1.0" encoding="utf-8"?>
    <inset xmlns:android="http://schemas.android.com/apk/res/android"
        android:insetLeft="16dp"
        android:insetTop="16dp"
        android:insetRight="16dp"
        android:insetBottom="16dp">
        <shape android:shape="rectangle">
            <corners android:radius="?dialogCornerRadius" />
            <solid android:color="?android:colorBackground" />
        </shape>
    </inset>
    
    0 讨论(0)
  • 2020-11-22 09:30

    It can done simply by using the Builder's setView(). You can create any view of your choice and feed into the builder. This works good. I use a custom TextView that is rendered by the dialog builder. I dont set the message and this space is utilized to render my custome textview.

    0 讨论(0)
  • 2020-11-22 09:32

    I"m not sure how Arve's solution would work in a custom Dialog with builder where the view is inflated via a LayoutInflator.

    The solution should be to insert the the ContextThemeWrapper in the inflator through cloneInContext():

    View sensorView = LayoutInflater.from(context).cloneInContext(
         new ContextThemeWrapper(context, R.style.AppTheme_DialogLight)
    ).inflate(R.layout.dialog_fingerprint, null);
    
    0 讨论(0)
提交回复
热议问题