如何更改AlertDialog标题的颜色以及下面的线条的颜色

时间:2022-10-20 20:12:26

I changed the color of an AlertDialog title using this command

我使用这个命令更改了AlertDialog标题的颜色

alert.setTitle( Html.fromHtml("<font color='#FF7F27'>Set IP Address</font>"));

But I want to change the color of the line that appear under the title; how can I do that ?

但是我想改变标题下的线的颜色;我怎么做呢?

Note: I don't want to use a custom layout

注意:我不想使用自定义布局

如何更改AlertDialog标题的颜色以及下面的线条的颜色

13 个解决方案

#1


129  

Unfortunately, this is not a particularly simple task to accomplish. In my answer here, I detail how to adjust the color of a ListSeparator by just checking out the parent style used by Android, creating a new image, and creating a new style based on the original. Unfortunately, unlike with the ListSeparator's style, AlertDialog themes are internal, and therefore cannot be referenced as parent styles. There is no easy way to change that little blue line! Thus you need to resort to making custom dialogs.

不幸的是,这并不是一个特别简单的任务。在我的回答中,我详细介绍了如何通过检查Android使用的父样式来调整列表分隔符的颜色,创建新的图像,并基于原始样式创建新的样式。不幸的是,与ListSeparator的样式不同,AlertDialog主题是内部的,因此不能作为父样式引用。要改变这条蓝色的线是不容易的!因此,您需要使用定制的对话框。

If that just isn't your cup of tea... don't give up! I was very disturbed that there was no easy way to do this so I set up a little project on github for making quickly customized holo-style dialogs (assuming that the phone supports the Holo style). You can find the project here: https://github.com/danoz73/QustomDialog

如果那不是你喜欢的……别放弃!我很担心没有简单的方法可以做到这一点,所以我在github上建立了一个小项目,用来制作快速定制的Holo风格的对话框(假设手机支持Holo风格)。您可以在这里找到这个项目:https://github.com/danoz73/QustomDialog

It should easily enable going from boring blue to exciting orange!

它应该很容易使人从乏味的蓝色到令人兴奋的橙色!

如何更改AlertDialog标题的颜色以及下面的线条的颜色

The project is basically an example of using a custom dialog builder, and in the example I created a custom view that seemed to cater to the IP Address example you give in your original question.

这个项目基本上是使用自定义对话框构建器的一个示例,在这个示例中,我创建了一个自定义视图,它似乎符合您在原始问题中给出的IP地址示例。

With QustomDialog, in order to create a basic dialog (title, message) with a desired different color for the title or divider, you use the following code:

使用QustomDialog,为了创建一个基本的对话框(标题、消息),标题或分隔符需要不同的颜色,您可以使用以下代码:

private String HALLOWEEN_ORANGE = "#FF7F27";

QustomDialogBuilder qustomDialogBuilder = new QustomDialogBuilder(v.getContext()).
    setTitle("Set IP Address").
    setTitleColor(HALLOWEEN_ORANGE).
    setDividerColor(HALLOWEEN_ORANGE).
    setMessage("You are now entering the 10th dimension.");

qustomDialogBuilder.show();

And in order to add a custom layout (say, to add the little IP address EditText), you add

为了添加一个自定义布局(例如,添加小的IP地址EditText),您可以添加。

setCustomView(R.layout.example_ip_address_layout, v.getContext())

to the builder with a layout that you have designed (the IP example can be found in the github). I hope this helps. Many thanks to Joseph Earl and his answer here.

对于您设计的布局的构建器(可以在github中找到IP示例)。我希望这可以帮助。非常感谢约瑟夫·厄尔和他的回答。

#2


72  

Divider color:

分频器颜色:

It is a hack a bit, but it works great for me and it works without any external library (at least on Android 4.4).

这是一个小技巧,但对我来说非常有用,而且它不需要任何外部库(至少在Android 4.4上是这样)。

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.dialog)
       .setIcon(R.drawable.ic)
       .setMessage(R.string.dialog_msg);
//The tricky part
Dialog d = builder.show();
int dividerId = d.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
View divider = d.findViewById(dividerId);
divider.setBackgroundColor(getResources().getColor(R.color.my_color));

You can find more dialog's ids in alert_dialog.xml file. Eg. android:id/alertTitle for changing title color...

在alert_dialog中可以找到更多对话框的id。xml文件。如。android:id/alertTitle可以更改标题颜色……

UPDATE: Title color

更新:标题颜色

Hack for changing title color:

修改标题颜色的技巧:

int textViewId = d.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
TextView tv = (TextView) d.findViewById(textViewId);
tv.setTextColor(getResources().getColor(R.color.my_color));

#3


21  

check this is useful for you...

这对你很有用……

public void setCustomTitle (View customTitleView)

you get detail from following link.

您可以从以下链接获得详细信息。

http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setCustomTitle%28android.view.View%29

http://developer.android.com/reference/android/app/AlertDialog.Builder.html setCustomTitle % 28 android.view.view % 29

CustomDialog.java

CustomDialog.java

Dialog alert = new Dialog(this);
    alert.requestWindowFeature(Window.FEATURE_NO_TITLE);
    alert.setContentView(R.layout.title);
    TextView msg = (TextView)alert.findViewById(R.id.textView1);
    msg.setText("Hello Friends.\nIP address : 111.111.1.111");
    alert.show();

title.xml

title.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Set IP address"
    android:textColor="#ff0000"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<ImageView 
    android:layout_width="fill_parent"
    android:layout_height="2dp"
    android:layout_marginTop="5dp"
    android:background="#00ff00"
    />
<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#775500"
    android:textAppearance="?android:attr/textAppearanceLarge" />

如何更改AlertDialog标题的颜色以及下面的线条的颜色

#4


10  

This will set the color for the title, icon, and divider. Bound to change with any new Android version.

这将设置标题、图标和分隔符的颜色。任何新的Android版本都会改变。

public static void colorAlertDialogTitle(AlertDialog dialog, int color) {
    int dividerId = dialog.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
    if (dividerId != 0) {
        View divider = dialog.findViewById(dividerId);
        divider.setBackgroundColor(color);
    }

    int textViewId = dialog.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
    if (textViewId != 0) {
        TextView tv = (TextView) dialog.findViewById(textViewId);
        tv.setTextColor(color);
    }

    int iconId = dialog.getContext().getResources().getIdentifier("android:id/icon", null, null);
    if (iconId != 0) {
        ImageView icon = (ImageView) dialog.findViewById(iconId);
        icon.setColorFilter(color);
    }
}

Remember to call dialog.show() before calling this method.

在调用此方法之前,请记住调用dialog.show()。

#5


5  

By following the Dialog source code, I found that Title is generated in Class MidWindow by inflating the dialog_title_holo.xml layout. so the Id of mTitleView is title and the Id of divider is titleDivider.

通过遵循对话框的源代码,我发现Title是通过填充dialog_title_holo在类中间窗口中生成的。xml布局。所以mTitleView的Id是title,而divider的Id是titleDivider。

we can access to Id of title simply by android.R.id.title.

我们可以通过android.R.id.title访问标题Id。

and access to Id of titleDivider by Resources.getSystem().getIdentifier("titleDivider","id", "android");

通过Resources.getSystem()访问titleDivider的Id。getIdentifier(“titleDivider”、“id”,“android”);

The final code that i used to change the Direction of title and changing color is:

最后我用来改变标题方向和颜色的代码是:

TextView mTitle = (TextView)findViewById(android.R.id.title);
mTitle.setGravity(Gravity.RIGHT|Gravity.CENTER_VERTICAL);
int x = Resources.getSystem().getIdentifier("titleDivider","id", "android");
View titleDivider = findViewById(x);
titleDivider.setBackgroundColor(getContext().getResources().getColor(R.color.some_color));

#6


3  

If you don't want a "library" for that, you can use this badly hack:

如果你不想要一个“图书馆”,你可以用这个坏办法:

((ViewGroup)((ViewGroup)getDialog().getWindow().getDecorView()).getChildAt(0)) //ie LinearLayout containing all the dialog (title, titleDivider, content)
.getChildAt(1) // ie the view titleDivider
.setBackgroundColor(getResources().getColor(R.color.yourBeautifulColor));

This was tested and work on 4.x; not tested under, but if my memory is good it should work for 2.x and 3.x

这是测试和工作的4倍;没有在下面测试,但如果我的记忆力很好,它应该适用于2。倍和3.倍

#7


2  

If you are creating custom Layout for alert dialog

如果您正在为警报对话框创建自定义布局

then you may add like this way easily to change the color

然后你可以像这样很容易地添加以改变颜色

<LinearLayout
    android:id="@+id/DialogTitleBorder"
    android:layout_width="fill_parent"
    android:layout_height="1dip"
    android:layout_below="@id/mExitDialogDesc"
    android:background="#4BBAE3"            <!--change color easily -->
    >

</LinearLayout>

#8


1  

If your using custom title layout then you can use it like alertDialog.setCustomTitle(customTitle);

如果您使用自定义标题布局,那么您可以像alertDialog.setCustomTitle(customTitle)那样使用它;

For example

例如

on UI thread used dialog like 

 LayoutInflater inflater=LayoutInflater.from(getApplicationContext());
 View customTitle=inflater.inflate(R.layout.customtitlebar, null);
 AlertDialog.Builder d=new AlertDialog.Builder(this);
 d.setCustomTitle(customTitle);
 d.setMessage("Message");
 d.setNeutralButton("OK", null);
 d.show();


customtitlebar.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="#525f67">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/ic_launcher"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true" >
    </ImageView>

    <TextView
        android:id="@+id/customtitlebar"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:textColor="#ffffff"
        android:text="Title Name"
        android:padding="3px"
        android:textStyle="bold" 
        android:layout_toRightOf="@id/icon"
        android:layout_alignParentTop="true"
        android:gravity="center_vertical"/>

     <ImageView
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#ff0000" 
        android:layout_below="@id/icon"><!-- This is line below the title -->
    </ImageView>

</RelativeLayout>

#9


0  

Continuing from this answer: https://*.com/a/15285514/1865860, I forked the nice github repo from @daniel-smith and made some improvements:

继续回答:https://*.com/a/15285514/1865860,我将@daniel-smith的漂亮github repo与@daniel-smith分开,做了一些改进:

  • improved example Activity
  • 改进的例子活动
  • improved layouts
  • 改进的布局
  • fixed setItems method
  • 固定setItems方法
  • added dividers into items_list
  • 添加分隔器到items_list
  • dismiss dialogs on click
  • 把对话框上点击
  • support for disabled items in setItems methods
  • 支持在setItems方法中禁用的项目
  • listItem touch feedback
  • 列联系反馈
  • scrollable dialog message
  • 可滚动对话框消息

link: https://github.com/dentex/QustomDialog

链接:https://github.com/dentex/QustomDialog

#10


0  

I came up with another solution that handles the styling of your dialogs in one place and you don't have to worry about when you apply it - dialog show/not shown, which can cause an error (should call requestFocus or something like that ;P).

我找到了另一种解决方案,它可以在一个地方处理您的对话框的样式,您不必担心应用它的时候——对话框显示/未显示,这会导致错误(应该调用requestFocus或类似的东西;P)。

Usage example:

使用的例子:

AlertDialog.Builder builder = new AlertDialog.Builder(context);
AlertDialog dialog = builder.create(); //or builder.show()
DialogViewDecorator.decorate(dialog, android.R.color.holo_red_light); //can also set the defaut color in the class

Implementation:

实现:

public class DialogViewDecorator {

private static final
@ColorRes int DEFAULT_TITLE_DIVIDER_COLOR = android.R.color.holo_orange_light;

public static void decorate(Dialog dialog) {
    decorate(dialog, DEFAULT_TITLE_DIVIDER_COLOR);
}

/**
 * Sets the title divider color when the view is shown by setting DialogInterface.OnShowListener on the dialog.
 * <p/>
 * If you want to do other things onShow be sure to extend OnDecoratedDialogShownListener(call super.show(...)!)
 * and call {@link #decorate(Dialog, int, OnDecoratedDialogShownListener)}.
 *
 * @param dialog
 * @param titleDividerColor
 */
public static void decorate(Dialog dialog, final int titleDividerColor) {
    decorate(dialog, titleDividerColor, new OnDecoratedDialogShownListener(titleDividerColor));
}

/**
 * Method for setting a extended implementation of OnDecoratedDialogShownListener. Don't forget to call super
 * or the titleDividerColor wont be applied!
 *
 * @param dialog
 * @param titleDividerColor
 * @param OnShowListener
 * @param <T>
 */
public static <T extends OnDecoratedDialogShownListener> void decorate(Dialog dialog, int titleDividerColor, T OnShowListener) {
    if (dialog == null || titleDividerColor <= 0) { return; }

    if (dialog.isShowing()) {
        setTitleDividerColor(dialog, titleDividerColor);
    } else {
        dialog.setOnShowListener(OnShowListener);
    }
}

private static void setTitleDividerColor(DialogInterface dialogInterface, int titleDividerColor) {
    try {
        Dialog dialog = (Dialog) dialogInterface;
        int dividerId = dialog.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
        View divider = dialog.findViewById(dividerId);
        if (divider != null) {
            divider.setBackgroundColor(dialog.getContext().getResources().getColor(titleDividerColor));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}


public static class OnDecoratedDialogShownListener implements DialogInterface.OnShowListener {
    private int titleDividerColor;

    public OnDecoratedDialogShownListener() {
        this.titleDividerColor = DEFAULT_TITLE_DIVIDER_COLOR;
    }

    public OnDecoratedDialogShownListener(int titleDividerColor) {
        this.titleDividerColor = titleDividerColor;
    }

    @Override
    public void onShow(DialogInterface dialogInterface) {
        setTitleDividerColor(dialogInterface, titleDividerColor);
    }
}}

#11


0  

In the class onCreateView, I put this:

在onCreateView类中,我这样写:

Dialog d = getDialog();
    d.setTitle(Html.fromHtml("<font color='#EC407A'>About</font>"));
    int dividerId = d.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
    View divider = d.findViewById(dividerId);
    divider.setBackgroundColor(getResources().getColor(R.color.colorPrimary));

colorPrimary links to our colors.xml file that stores all the colors. Also d.setTitle provides a hacky way to set the title colour.

colorPrimary链接到我们的颜色。存储所有颜色的xml文件。d。setTitle提供了一种设置标题颜色的简单方法。

#12


0  

Instead of using divider in dialog, use the view in the custom layout and set the layout as custom layout in dialog.

不要在对话框中使用分隔符,在自定义布局中使用视图,并在对话框中将布局设置为自定义布局。

custom_popup.xml:

custom_popup.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

    <com.divago.view.TextViewMedium
        android:id="@+id/txtTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingBottom="10dp"
        android:paddingTop="10dp"
        android:text="AlertDialog"
        android:textColor="@android:color/black"
        android:textSize="20sp" />

    <View
        android:id="@+id/border"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_below="@id/txtTitle"
        android:background="@color/txt_dark_grey" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/border"
        android:scrollbars="vertical">

        <com.divago.view.TextViewRegular
            android:id="@+id/txtPopup"
            android:layout_margin="15dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </ScrollView>
</RelativeLayout>

activity.java:

activity.java:

public void showPopUp(String title, String text) {

    LayoutInflater inflater = getLayoutInflater();
    View alertLayout = inflater.inflate(R.layout.custom_popup, null);

    TextView txtContent = alertLayout.findViewById(R.id.txtPopup);
    txtContent.setText(text);

    TextView txtTitle = alertLayout.findViewById(R.id.txtTitle);
    txtTitle.setText(title);

    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setView(alertLayout);
    alert.setCancelable(true);

    alert.setPositiveButton("Done", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });

    AlertDialog dialog = alert.create();
    dialog.show();
}

#13


0  

In case you are using extending the dialog the use:

如果您正在使用扩展对话框,请使用:

requestWindowFeature(Window.FEATURE_NO_TITLE);

#1


129  

Unfortunately, this is not a particularly simple task to accomplish. In my answer here, I detail how to adjust the color of a ListSeparator by just checking out the parent style used by Android, creating a new image, and creating a new style based on the original. Unfortunately, unlike with the ListSeparator's style, AlertDialog themes are internal, and therefore cannot be referenced as parent styles. There is no easy way to change that little blue line! Thus you need to resort to making custom dialogs.

不幸的是,这并不是一个特别简单的任务。在我的回答中,我详细介绍了如何通过检查Android使用的父样式来调整列表分隔符的颜色,创建新的图像,并基于原始样式创建新的样式。不幸的是,与ListSeparator的样式不同,AlertDialog主题是内部的,因此不能作为父样式引用。要改变这条蓝色的线是不容易的!因此,您需要使用定制的对话框。

If that just isn't your cup of tea... don't give up! I was very disturbed that there was no easy way to do this so I set up a little project on github for making quickly customized holo-style dialogs (assuming that the phone supports the Holo style). You can find the project here: https://github.com/danoz73/QustomDialog

如果那不是你喜欢的……别放弃!我很担心没有简单的方法可以做到这一点,所以我在github上建立了一个小项目,用来制作快速定制的Holo风格的对话框(假设手机支持Holo风格)。您可以在这里找到这个项目:https://github.com/danoz73/QustomDialog

It should easily enable going from boring blue to exciting orange!

它应该很容易使人从乏味的蓝色到令人兴奋的橙色!

如何更改AlertDialog标题的颜色以及下面的线条的颜色

The project is basically an example of using a custom dialog builder, and in the example I created a custom view that seemed to cater to the IP Address example you give in your original question.

这个项目基本上是使用自定义对话框构建器的一个示例,在这个示例中,我创建了一个自定义视图,它似乎符合您在原始问题中给出的IP地址示例。

With QustomDialog, in order to create a basic dialog (title, message) with a desired different color for the title or divider, you use the following code:

使用QustomDialog,为了创建一个基本的对话框(标题、消息),标题或分隔符需要不同的颜色,您可以使用以下代码:

private String HALLOWEEN_ORANGE = "#FF7F27";

QustomDialogBuilder qustomDialogBuilder = new QustomDialogBuilder(v.getContext()).
    setTitle("Set IP Address").
    setTitleColor(HALLOWEEN_ORANGE).
    setDividerColor(HALLOWEEN_ORANGE).
    setMessage("You are now entering the 10th dimension.");

qustomDialogBuilder.show();

And in order to add a custom layout (say, to add the little IP address EditText), you add

为了添加一个自定义布局(例如,添加小的IP地址EditText),您可以添加。

setCustomView(R.layout.example_ip_address_layout, v.getContext())

to the builder with a layout that you have designed (the IP example can be found in the github). I hope this helps. Many thanks to Joseph Earl and his answer here.

对于您设计的布局的构建器(可以在github中找到IP示例)。我希望这可以帮助。非常感谢约瑟夫·厄尔和他的回答。

#2


72  

Divider color:

分频器颜色:

It is a hack a bit, but it works great for me and it works without any external library (at least on Android 4.4).

这是一个小技巧,但对我来说非常有用,而且它不需要任何外部库(至少在Android 4.4上是这样)。

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.dialog)
       .setIcon(R.drawable.ic)
       .setMessage(R.string.dialog_msg);
//The tricky part
Dialog d = builder.show();
int dividerId = d.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
View divider = d.findViewById(dividerId);
divider.setBackgroundColor(getResources().getColor(R.color.my_color));

You can find more dialog's ids in alert_dialog.xml file. Eg. android:id/alertTitle for changing title color...

在alert_dialog中可以找到更多对话框的id。xml文件。如。android:id/alertTitle可以更改标题颜色……

UPDATE: Title color

更新:标题颜色

Hack for changing title color:

修改标题颜色的技巧:

int textViewId = d.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
TextView tv = (TextView) d.findViewById(textViewId);
tv.setTextColor(getResources().getColor(R.color.my_color));

#3


21  

check this is useful for you...

这对你很有用……

public void setCustomTitle (View customTitleView)

you get detail from following link.

您可以从以下链接获得详细信息。

http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setCustomTitle%28android.view.View%29

http://developer.android.com/reference/android/app/AlertDialog.Builder.html setCustomTitle % 28 android.view.view % 29

CustomDialog.java

CustomDialog.java

Dialog alert = new Dialog(this);
    alert.requestWindowFeature(Window.FEATURE_NO_TITLE);
    alert.setContentView(R.layout.title);
    TextView msg = (TextView)alert.findViewById(R.id.textView1);
    msg.setText("Hello Friends.\nIP address : 111.111.1.111");
    alert.show();

title.xml

title.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Set IP address"
    android:textColor="#ff0000"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<ImageView 
    android:layout_width="fill_parent"
    android:layout_height="2dp"
    android:layout_marginTop="5dp"
    android:background="#00ff00"
    />
<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#775500"
    android:textAppearance="?android:attr/textAppearanceLarge" />

如何更改AlertDialog标题的颜色以及下面的线条的颜色

#4


10  

This will set the color for the title, icon, and divider. Bound to change with any new Android version.

这将设置标题、图标和分隔符的颜色。任何新的Android版本都会改变。

public static void colorAlertDialogTitle(AlertDialog dialog, int color) {
    int dividerId = dialog.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
    if (dividerId != 0) {
        View divider = dialog.findViewById(dividerId);
        divider.setBackgroundColor(color);
    }

    int textViewId = dialog.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
    if (textViewId != 0) {
        TextView tv = (TextView) dialog.findViewById(textViewId);
        tv.setTextColor(color);
    }

    int iconId = dialog.getContext().getResources().getIdentifier("android:id/icon", null, null);
    if (iconId != 0) {
        ImageView icon = (ImageView) dialog.findViewById(iconId);
        icon.setColorFilter(color);
    }
}

Remember to call dialog.show() before calling this method.

在调用此方法之前,请记住调用dialog.show()。

#5


5  

By following the Dialog source code, I found that Title is generated in Class MidWindow by inflating the dialog_title_holo.xml layout. so the Id of mTitleView is title and the Id of divider is titleDivider.

通过遵循对话框的源代码,我发现Title是通过填充dialog_title_holo在类中间窗口中生成的。xml布局。所以mTitleView的Id是title,而divider的Id是titleDivider。

we can access to Id of title simply by android.R.id.title.

我们可以通过android.R.id.title访问标题Id。

and access to Id of titleDivider by Resources.getSystem().getIdentifier("titleDivider","id", "android");

通过Resources.getSystem()访问titleDivider的Id。getIdentifier(“titleDivider”、“id”,“android”);

The final code that i used to change the Direction of title and changing color is:

最后我用来改变标题方向和颜色的代码是:

TextView mTitle = (TextView)findViewById(android.R.id.title);
mTitle.setGravity(Gravity.RIGHT|Gravity.CENTER_VERTICAL);
int x = Resources.getSystem().getIdentifier("titleDivider","id", "android");
View titleDivider = findViewById(x);
titleDivider.setBackgroundColor(getContext().getResources().getColor(R.color.some_color));

#6


3  

If you don't want a "library" for that, you can use this badly hack:

如果你不想要一个“图书馆”,你可以用这个坏办法:

((ViewGroup)((ViewGroup)getDialog().getWindow().getDecorView()).getChildAt(0)) //ie LinearLayout containing all the dialog (title, titleDivider, content)
.getChildAt(1) // ie the view titleDivider
.setBackgroundColor(getResources().getColor(R.color.yourBeautifulColor));

This was tested and work on 4.x; not tested under, but if my memory is good it should work for 2.x and 3.x

这是测试和工作的4倍;没有在下面测试,但如果我的记忆力很好,它应该适用于2。倍和3.倍

#7


2  

If you are creating custom Layout for alert dialog

如果您正在为警报对话框创建自定义布局

then you may add like this way easily to change the color

然后你可以像这样很容易地添加以改变颜色

<LinearLayout
    android:id="@+id/DialogTitleBorder"
    android:layout_width="fill_parent"
    android:layout_height="1dip"
    android:layout_below="@id/mExitDialogDesc"
    android:background="#4BBAE3"            <!--change color easily -->
    >

</LinearLayout>

#8


1  

If your using custom title layout then you can use it like alertDialog.setCustomTitle(customTitle);

如果您使用自定义标题布局,那么您可以像alertDialog.setCustomTitle(customTitle)那样使用它;

For example

例如

on UI thread used dialog like 

 LayoutInflater inflater=LayoutInflater.from(getApplicationContext());
 View customTitle=inflater.inflate(R.layout.customtitlebar, null);
 AlertDialog.Builder d=new AlertDialog.Builder(this);
 d.setCustomTitle(customTitle);
 d.setMessage("Message");
 d.setNeutralButton("OK", null);
 d.show();


customtitlebar.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="#525f67">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/ic_launcher"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true" >
    </ImageView>

    <TextView
        android:id="@+id/customtitlebar"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:textColor="#ffffff"
        android:text="Title Name"
        android:padding="3px"
        android:textStyle="bold" 
        android:layout_toRightOf="@id/icon"
        android:layout_alignParentTop="true"
        android:gravity="center_vertical"/>

     <ImageView
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#ff0000" 
        android:layout_below="@id/icon"><!-- This is line below the title -->
    </ImageView>

</RelativeLayout>

#9


0  

Continuing from this answer: https://*.com/a/15285514/1865860, I forked the nice github repo from @daniel-smith and made some improvements:

继续回答:https://*.com/a/15285514/1865860,我将@daniel-smith的漂亮github repo与@daniel-smith分开,做了一些改进:

  • improved example Activity
  • 改进的例子活动
  • improved layouts
  • 改进的布局
  • fixed setItems method
  • 固定setItems方法
  • added dividers into items_list
  • 添加分隔器到items_list
  • dismiss dialogs on click
  • 把对话框上点击
  • support for disabled items in setItems methods
  • 支持在setItems方法中禁用的项目
  • listItem touch feedback
  • 列联系反馈
  • scrollable dialog message
  • 可滚动对话框消息

link: https://github.com/dentex/QustomDialog

链接:https://github.com/dentex/QustomDialog

#10


0  

I came up with another solution that handles the styling of your dialogs in one place and you don't have to worry about when you apply it - dialog show/not shown, which can cause an error (should call requestFocus or something like that ;P).

我找到了另一种解决方案,它可以在一个地方处理您的对话框的样式,您不必担心应用它的时候——对话框显示/未显示,这会导致错误(应该调用requestFocus或类似的东西;P)。

Usage example:

使用的例子:

AlertDialog.Builder builder = new AlertDialog.Builder(context);
AlertDialog dialog = builder.create(); //or builder.show()
DialogViewDecorator.decorate(dialog, android.R.color.holo_red_light); //can also set the defaut color in the class

Implementation:

实现:

public class DialogViewDecorator {

private static final
@ColorRes int DEFAULT_TITLE_DIVIDER_COLOR = android.R.color.holo_orange_light;

public static void decorate(Dialog dialog) {
    decorate(dialog, DEFAULT_TITLE_DIVIDER_COLOR);
}

/**
 * Sets the title divider color when the view is shown by setting DialogInterface.OnShowListener on the dialog.
 * <p/>
 * If you want to do other things onShow be sure to extend OnDecoratedDialogShownListener(call super.show(...)!)
 * and call {@link #decorate(Dialog, int, OnDecoratedDialogShownListener)}.
 *
 * @param dialog
 * @param titleDividerColor
 */
public static void decorate(Dialog dialog, final int titleDividerColor) {
    decorate(dialog, titleDividerColor, new OnDecoratedDialogShownListener(titleDividerColor));
}

/**
 * Method for setting a extended implementation of OnDecoratedDialogShownListener. Don't forget to call super
 * or the titleDividerColor wont be applied!
 *
 * @param dialog
 * @param titleDividerColor
 * @param OnShowListener
 * @param <T>
 */
public static <T extends OnDecoratedDialogShownListener> void decorate(Dialog dialog, int titleDividerColor, T OnShowListener) {
    if (dialog == null || titleDividerColor <= 0) { return; }

    if (dialog.isShowing()) {
        setTitleDividerColor(dialog, titleDividerColor);
    } else {
        dialog.setOnShowListener(OnShowListener);
    }
}

private static void setTitleDividerColor(DialogInterface dialogInterface, int titleDividerColor) {
    try {
        Dialog dialog = (Dialog) dialogInterface;
        int dividerId = dialog.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
        View divider = dialog.findViewById(dividerId);
        if (divider != null) {
            divider.setBackgroundColor(dialog.getContext().getResources().getColor(titleDividerColor));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}


public static class OnDecoratedDialogShownListener implements DialogInterface.OnShowListener {
    private int titleDividerColor;

    public OnDecoratedDialogShownListener() {
        this.titleDividerColor = DEFAULT_TITLE_DIVIDER_COLOR;
    }

    public OnDecoratedDialogShownListener(int titleDividerColor) {
        this.titleDividerColor = titleDividerColor;
    }

    @Override
    public void onShow(DialogInterface dialogInterface) {
        setTitleDividerColor(dialogInterface, titleDividerColor);
    }
}}

#11


0  

In the class onCreateView, I put this:

在onCreateView类中,我这样写:

Dialog d = getDialog();
    d.setTitle(Html.fromHtml("<font color='#EC407A'>About</font>"));
    int dividerId = d.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
    View divider = d.findViewById(dividerId);
    divider.setBackgroundColor(getResources().getColor(R.color.colorPrimary));

colorPrimary links to our colors.xml file that stores all the colors. Also d.setTitle provides a hacky way to set the title colour.

colorPrimary链接到我们的颜色。存储所有颜色的xml文件。d。setTitle提供了一种设置标题颜色的简单方法。

#12


0  

Instead of using divider in dialog, use the view in the custom layout and set the layout as custom layout in dialog.

不要在对话框中使用分隔符,在自定义布局中使用视图,并在对话框中将布局设置为自定义布局。

custom_popup.xml:

custom_popup.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

    <com.divago.view.TextViewMedium
        android:id="@+id/txtTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingBottom="10dp"
        android:paddingTop="10dp"
        android:text="AlertDialog"
        android:textColor="@android:color/black"
        android:textSize="20sp" />

    <View
        android:id="@+id/border"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_below="@id/txtTitle"
        android:background="@color/txt_dark_grey" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/border"
        android:scrollbars="vertical">

        <com.divago.view.TextViewRegular
            android:id="@+id/txtPopup"
            android:layout_margin="15dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </ScrollView>
</RelativeLayout>

activity.java:

activity.java:

public void showPopUp(String title, String text) {

    LayoutInflater inflater = getLayoutInflater();
    View alertLayout = inflater.inflate(R.layout.custom_popup, null);

    TextView txtContent = alertLayout.findViewById(R.id.txtPopup);
    txtContent.setText(text);

    TextView txtTitle = alertLayout.findViewById(R.id.txtTitle);
    txtTitle.setText(title);

    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setView(alertLayout);
    alert.setCancelable(true);

    alert.setPositiveButton("Done", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });

    AlertDialog dialog = alert.create();
    dialog.show();
}

#13


0  

In case you are using extending the dialog the use:

如果您正在使用扩展对话框,请使用:

requestWindowFeature(Window.FEATURE_NO_TITLE);