AlertDialog styling - how to change style (color) of title, message, etc

前端 未结 8 1361
醉梦人生
醉梦人生 2020-12-04 14:31

I\'ve breaking my head over this quite a bit. What I need to do is, change the style of all AlertDialogs in my android application - dialog background needs to

相关标签:
8条回答
  • 2020-12-04 14:52

    Here's my Code to theme the alert dialog box:

    <style name="alertDialog" parent="Theme.AppCompat.Dialog.Alert">
        <item name="android:background">@color/light_button_text_color</item>
        <item name="android:textColor">@android:color/black</item>
        <item name="android:textColorPrimary">@android:color/black</item>
        <item name="android:textColorSecondary">@android:color/black</item>
        <item name="android:titleTextColor" tools:targetApi="m">@android:color/black</item>
    </style>
    

    Place this code in styles.xml. In your java apply this theme as:

    AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.alertDialog);
    

    Output of the code

    0 讨论(0)
  • 2020-12-04 14:52

    I changed color programmatically in this way :

    var builder = new AlertDialog.Builder (this);
    ...
    ...
    ...
    var dialog = builder.Show ();
    int textColorId = Resources.GetIdentifier ("alertTitle", "id", "android");
    TextView textColor = dialog.FindViewById<TextView> (textColorId);
    textColor?.SetTextColor (Color.DarkRed);
    

    as alertTitle, you can change other data by this way (next example is for titleDivider):

    int titleDividerId = Resources.GetIdentifier ("titleDivider", "id", "android");
    View titleDivider = dialog.FindViewById (titleDividerId);
    titleDivider?.SetBackgroundColor (Color.Red);
    

    this is in C#, but in java it is the same.

    0 讨论(0)
  • 2020-12-04 14:59

    You need to define a Theme for your AlertDialog and reference it in your Activity's theme. The attribute is alertDialogTheme and not alertDialogStyle. Like this:

    <style name="Theme.YourTheme" parent="@android:style/Theme.Holo">
        ...
        <item name="android:alertDialogTheme">@style/YourAlertDialogTheme</item>
    </style>
    
    <style name="YourAlertDialogTheme">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:windowMinWidthMajor">@android:dimen/dialog_min_width_major</item>
        <item name="android:windowMinWidthMinor">@android:dimen/dialog_min_width_minor</item>
        <item name="android:windowTitleStyle">...</item>
        <item name="android:textAppearanceMedium">...</item>
        <item name="android:borderlessButtonStyle">...</item>
        <item name="android:buttonBarStyle">...</item>
    </style>
    

    You'll be able to change color and text appearance for the title, the message and you'll have some control on the background of each area. I wrote a blog post detailing the steps to style an AlertDialog.

    0 讨论(0)
  • 2020-12-04 15:02

    You have to add the style to the constructor of the dialog

    builder = new AlertDialog.Builder(this, R.style.DialogStyle);
    
    0 讨论(0)
  • 2020-12-04 15:12

    Use this in your Style in your values-v21/style.xml

    <style name="AlertDialogCustom" parent="@android:style/Theme.Material.Dialog.NoActionBar">
            <item name="android:windowBackground">@android:color/white</item>
            <item name="android:windowActionBar">false</item>
            <item name="android:colorAccent">@color/cbt_ui_primary_dark</item>
            <item name="android:windowTitleStyle">@style/DialogWindowTitle.Sphinx</item>
            <item name="android:textColorPrimary">@color/cbt_hints_color</item>
            <item name="android:backgroundDimEnabled">true</item>
            <item name="android:windowMinWidthMajor">@android:dimen/dialog_min_width_major</item>
            <item name="android:windowMinWidthMinor">@android:dimen/dialog_min_width_minor</item>
    </style>

    And for pre lollipop devices put it in values/style.xml

    <style name="AlertDialogCustom" parent="@android:style/Theme.Material.Dialog.NoActionBar">
            <item name="android:windowBackground">@android:color/white</item>
            <item name="android:windowActionBar">false</item>
            <item name="android:colorAccent">@color/cbt_ui_primary_dark</item>
            <item name="android:windowTitleStyle">@style/DialogWindowTitle.Sphinx</item>
            <item name="android:textColorPrimary">@color/cbt_hints_color</item>
            <item name="android:backgroundDimEnabled">true</item>
            <item name="android:windowMinWidthMajor">@android:dimen/dialog_min_width_major</item>
            <item name="android:windowMinWidthMinor">@android:dimen/dialog_min_width_minor</item>
    </style>
    
    <style name="DialogWindowTitle.Sphinx" parent="@style/DialogWindowTitle_Holo">
           <item name="android:textAppearance">@style/TextAppearance.Sphinx.DialogWindowTitle</item>
    </style>
    
    <style name="TextAppearance.Sphinx.DialogWindowTitle" parent="@android:style/TextAppearance.Holo.DialogWindowTitle">
            <item name="android:textColor">@color/dark</item>
            <!--<item name="android:fontFamily">sans-serif-condensed</item>-->
            <item name="android:textStyle">bold</item>
    </style>

    0 讨论(0)
  • 2020-12-04 15:14

    Remove the panel background

     <item name="android:windowBackground">@color/transparent_color</item> 
     <color name="transparent_color">#00000000</color>
    

    This is Mystyle:

     <style name="ThemeDialogCustom">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:windowBackground">@color/transparent_color</item>
        <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
        <item name="android:colorBackgroundCacheHint">@null</item>
    </style>
    

    Which i have added to the constructor.

    Add textColor :

    <item name="android:textColor">#ff0000</item>
    
    0 讨论(0)
提交回复
热议问题