简体   繁体   English

如何更改AlertDialog中的蓝线(Holo主题)

[英]how to change the blue line in the AlertDialog ( Holo Theme )

I want to change the color of the blue Title and blue line that comes in AlertDialog ( Holo Theme ), I was able to change the color of Title by looking into android styles.xml and themes.xml but i cannot find any property or style that i can use/change to change the color of my XML. 我想改变AlertDialog(Holo Theme)中出现的蓝色Title和蓝线的颜色,我可以通过查看android styles.xml和themes.xml来改变Title的颜色,但我找不到任何属性或样式我可以使用/更改来改变我的XML的颜色。

Following is the style that I am using to change the title color 以下是我用来更改标题颜色的样式

<style name="myAlertDialog" parent="android:Theme.Holo.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowTitleStyle">@style/myAlertDialogTitleStyle</item>
</style>

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

<style name="myAlertDialogTitleStyleTextAppearance">
    <item name="android:textSize">22sp</item>
    <item name="android:textColor">#ffff8800</item>
</style>

EDIT: I was mistaken in my original answer. 编辑:我在原来的答案中弄错了。

Unfortunately the styles used for dialogs are internal and cannot be used in the same manner as some of the other easy-to-manipulate holo elements. 不幸的是,用于对话框的样式是内部的,不能以与其他一些易于操作的holo元素相同的方式使用 I have created an open source project for dealing with this in very simple situations (I hope to extend it down the road and make it more.. legit). 我已经创建了一个开源项目,用于在非常简单的情况下处理这个问题 (我希望将其扩展到更远的路上并使其更加合法)。 Here is a link to a new answer that addresses this more in depth: How can I change the color of AlertDialog title and the color of the line under it 这是一个新答案的链接, 可以更深入地解决这个问题: 如何更改AlertDialog标题的颜色和其下的线条颜色


For posterity, here is my naive old answer: 对于后代,这是我天真的老回答:

Look at the answer posted here . 看看这里发布答案 For your particular goal, you will want to be overriding the dialogTitleDecorLayout style ( I think! ) as opposed to the listSeparatorTextViewStyle style. 为了您的特定目标,您将希望覆盖dialogTitleDecorLayout样式( 我认为! )而不是listSeparatorTextViewStyle样式。

If you are curious where this is originally set, you can poke around the themes.xml file in your android sdk folder on your computer and search for the dialogTitleDecorLayout attribute. 如果您对最初设置的位置感到好奇,可以在计算机上的android sdk文件夹中找到themes.xml文件并搜索dialogTitleDecorLayout属性。 This should lead you to a dialog_title_holo layout file which should also be on your computer. 这应该会引导您进入一个dialog_title_holo布局文件,该文件也应该在您的计算机上。 There you should find the following code within the layout: 你应该在布局中找到以下代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:fitsSystemWindows="true">
  <TextView android:id="@android:id/title" style="?android:attr/windowTitleStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="60dip"
    android:paddingLeft="32dip"
    android:paddingRight="32dip"
    android:gravity="center_vertical|left" />
  <ImageView android:id="@+id/titleDivider"
        android:layout_width="match_parent"
        android:layout_height="4dip"
        android:scaleType="fitXY"
        android:gravity="fill_horizontal"
        android:paddingLeft="16dip"
        android:paddingRight="16dip"
        android:src="@android:drawable/divider_strong_holo" />
  <FrameLayout
    android:layout_width="match_parent" android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical"
    android:foreground="?android:attr/windowContentOverlay">
    <FrameLayout android:id="@android:id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
  </FrameLayout>
</LinearLayout>

The code of note is the @android:drawable/divider_strong_holo reference. 注意的代码是@android:drawable/divider_strong_holo引用。 This file also exists on your computer and it should be a blue 9patch. 此文件也存在于您的计算机上,它应该是蓝色9patch。 You will need to create a similar 9patch of a different color. 您需要创建一个不同颜色的类似9patch。 Good Luck! 祝好运! Even if this isn't quite the right attribute I think you see what you may need to do here... 即使这不是一个正确的属性,我想你看到你可能需要在这里做些什么......

After some tinkering, I've come up with a way to re-brand the dialog the moment it is shown: 经过一些修修补补后,我想出了一种方法,可以在显示对话框时重新标记对话框:

private AlertDialog showWithThemeColors() {
    Log.d(TAG, "showWithThemeColors()");
    AlertDialog dialog = this.builder.show();
    try {
        ViewGroup decorView = (ViewGroup) dialog.getWindow().getDecorView();
        FrameLayout windowContentView = (FrameLayout) decorView.getChildAt(0);
        FrameLayout contentView = (FrameLayout) windowContentView.getChildAt(0);
        LinearLayout parentPanel = (LinearLayout) contentView.getChildAt(0);
        LinearLayout topPanel = (LinearLayout) parentPanel.getChildAt(0);
        View titleDivider = topPanel.getChildAt(2);
        LinearLayout titleTemplate = (LinearLayout) topPanel.getChildAt(1);
        TextView alertTitle = (TextView) titleTemplate.getChildAt(1);

        int textColor = this.context.getResources().getColor(R.color.customer_text_on_primary);
        alertTitle.setTextColor(textColor);

        int primaryColor = this.context.getResources().getColor(R.color.customer_primary);
        titleDivider.setBackgroundColor(primaryColor);
    } catch (Exception e) {
        Log.e(TAG, "showWithThemeColors() - Could not re-brand dialog with theme colors.", e);
    }

    return dialog;
}

Note, that this solution can break anytime the underlying system layout for the dialog changes. 请注意,此解决方案可随时更改对话框的基础系统布局。 However, it is the original system dialog with all the advantages of such a solution. 但是,这是原始系统对话框,具有此类解决方案的所有优点。

There is a library which does exactly that for all your dialogs: 有一个库可以完全满足您的所有对话框:

https://github.com/inmite/android-styled-dialogs https://github.com/inmite/android-styled-dialogs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM