如何更改Appcompat对话框的标题和标题分隔线颜色?

8
有没有办法更改Appcompat对话框标题和标题分隔符的颜色?我不想使用holo浅蓝色。
我找到了这个链接,但是它是针对holo light的,不能用于appcompat。
谢谢。
1个回答

33
改变对话框标题分隔符的唯一方法是通过子类化 Dialog,并使用 Resources.getIdentifier 找到标题分隔符 View。之后,您只需要调用 View.setBackgroundColor。由于这是自定义标题分隔符的唯一方法,因此您可能会使用相同的方法自定义标题颜色。
至于为什么您不能使您链接的答案起作用,很难说。您没有包括任何代码或您尝试过的内容,因此很难确定为什么您没有得到想要的结果。
以下是更改标题颜色和标题分隔符颜色的示例:
/**
 * 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();
}

使用 DialogFragmentAlertDialog.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");
}

结果

示例


谢谢。它可以工作,但是否有办法使用AlertDialog.Builder创建它?我正在使用DialogFragments来创建对话框。 - Vanama
@Vanama,我进行了编辑,并使用DialogFragmentAlertDialog.Builder提供了一个示例。 - adneal
4
提醒未来可能遇到此问题的用户,使用通用对话框时,标识符名称必须使用"title"而不是"alertTitle"。请注意,本翻译仅供参考,不得用于其他用途。 - zgc7009
4
非常好的答案。但是在使用appcompat AlertDialog时不再适用。有人知道我如何通过android.support.v7.app.AlertDialog获取标题吗?我尝试了很多次,但总是得到“null” :-( - chrisonline
title=null和titleDivider=null //编译'com.android.support:appcompat-v7:23.1.1' - Elron

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接