如何更改Appcompat对话框的标题和标题分辨率颜色? [英] How to change Appcompat dialog title and title divider color?

查看:342
本文介绍了如何更改Appcompat对话框的标题和标题分辨率颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法改变Appcompat对话框标题和标题分色器颜色?我不想使用全光蓝色。

Is there any way to change Appcompat dialog title and title divider color? I don't want to use holo light blue color.

我创立了这个链接,但是适用于全光,不适用于appcompat。

I founded this link but is for holo light and don't work with appcompat.

提前感谢

推荐答案

更改对话框标题分隔符颜色是通过子类化对话框并使用 Resources.getIdentifier 来查找标题分隔符查看。之后,您需要的是调用 View.setBackgroundColor 。由于这是自定义标题分隔线的唯一方法,您可以继续使用相同的方法自定义标题颜色。

The only way to change the Dialog title divider color is by subclassing Dialog and using Resources.getIdentifier to find the title divider View. After that all you need is a call to View.setBackgroundColor. Since this is the only way to customize the title divider, you may as well go ahead and use the same method to customize the title color as well.

但是,为什么你无法得到与您联系的答案,很难说。您不包括任何代码或您尝试过的任何内容,因此,您可能无法确定为什么没有收到您想要的结果。

But as far as why you can't get the answer you linked to working for you, it's hard to say. You don't include any code or anything you've tried, so that makes it tricky to pinpoint why you aren't receiving the results you want.

这是一个例子更改标题颜色和标题分隔符颜色:

Here's an example of changing the title color and the title divider color:

/**
 * A sublcass of {@link AlertDialog} used to customize the title and title
 * divider colors
 */
public class CustomDialog extends AlertDialog {

    /**
     * Constructor for <code>CustomDialog</code>
     * 
     * @param context The {@link Context} to use
     */
    public CustomDialog(Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Resources res = getContext().getResources();
        final int yellow = res.getColor(android.R.color.holo_orange_light);

        // Title
        final int titleId = res.getIdentifier("alertTitle", "id", "android");
        final View title = findViewById(titleId);
        if (title != null) {
            ((TextView) title).setTextColor(yellow);
        }

        // Title divider
        final int titleDividerId = res.getIdentifier("titleDivider", "id", "android");
        final View titleDivider = findViewById(titleDividerId);
        if (titleDivider != null) {
            titleDivider.setBackgroundColor(yellow);
        }
    }

}

实现

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final CustomDialog customDialog = new CustomDialog(this);
    customDialog.setTitle("Title");
    customDialog.setMessage("Message");
    customDialog.show();
}

使用 DialogFragment with AlertDialog.Builder

Using a DialogFragment with AlertDialog.Builder

public class CustomDialogFragment extends DialogFragment {

    /**
     * Empty constructor as per the {@link Fragment} docs
     */
    public CustomDialogFragment() {
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new AlertDialog.Builder(getActivity())
                .setTitle("Title")
                .setMessage("Message")
                .create();
    }

    @Override
    public void onStart() {
        super.onStart();
        final Resources res = getResources();
        final int yellow = res.getColor(android.R.color.holo_orange_light);

        // Title
        final int titleId = res.getIdentifier("alertTitle", "id", "android");
        final View title = getDialog().findViewById(titleId);
        if (title != null) {
            ((TextView) title).setTextColor(yellow);
        }

        // Title divider
        final int titleDividerId = res.getIdentifier("titleDivider", "id", "android");
        final View titleDivider = getDialog().findViewById(titleDividerId);
        if (titleDivider != null) {
            titleDivider.setBackgroundColor(yellow);
        }
    }

}

实现

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    new CustomDialogFragment().show(getFragmentManager(), "customDialogFragment");
}

结果

这篇关于如何更改Appcompat对话框的标题和标题分辨率颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆