What theme attributes do I need to override to change the blue highlight color of my dialogs?

蓝咒 提交于 2019-12-03 03:32:57

I went digging around the source and came across the alert_dialog_holo.xml layout file. This is the divider view:

<View android:id="@+id/titleDivider"
    android:layout_width="match_parent"
    android:layout_height="2dip"
    android:visibility="gone"
    android:background="@android:color/holo_blue_light" />

In the AlertController class, it's visibility is set to VISIBLE if there is a title present. Since the color is hardcoded, there's not going to be a way to overwrite this with a style attribute.

This is the title view:

<com.android.internal.widget.DialogTitle android:id="@+id/alertTitle"
    style="?android:attr/windowTitleStyle"
    android:singleLine="true"
    android:ellipsize="end"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

So it uses the windowTitleStyle. It took a lot of chasing, but I eventually found the style that uses:

<style name="TextAppearance.Holo.DialogWindowTitle">
    <item name="android:textSize">22sp</item>
    <item name="android:textColor">@android:color/holo_blue_light</item>
</style>

Following the parent styles back, I was able to change the text color only via styles:

<style name="AppTheme" parent="@android:style/Theme.Holo">
    <item name="android:alertDialogTheme">@style/AlertDialogStyle</item>
</style>

<style name="AlertDialogStyle" parent="@android:style/Theme.Holo.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</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">@style/DialogWindowTitle</item>
</style>

<style name="DialogWindowTitle">
    <item name="android:maxLines">1</item>
    <item name="android:scrollHorizontally">true</item>
    <item name="android:textAppearance">@style/DialogWindowTitleAppearance</item>
</style>

<style name="DialogWindowTitleAppearance" parent="@android:style/TextAppearance.Holo.DialogWindowTitle">
    <item name="android:textColor">#00ff00</item>
</style>

Now for the divider, you can't change it via styles, but since you know the id, you can extend the AlertDialog class and intercept it when it creates its layout (onCreate) and change it's color. Though this is handled in the private AlertController class, so I'm unsure how much luck you'll have. I'll look into this more and come back if I come up with anything.

I found a solution to remove the divider: just add the following line in your custom alert dialog style (please refer to AlertDialogStyle in Jason Robinson's answer):

    <item name="android:divider">@null</item>"

Then the divider will gone.

If you still want to add a custom divider, you can add it in your custom content layout.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!