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

Van*_*ama 8 android dialog android-appcompat

有没有办法更改Appcompat对话框标题和标题分隔颜色?我不想使用holo浅蓝色.

我创建了这个链接但是用于holo light并且不能与appcompat一起使用.

提前致谢.

adn*_*eal 33

更改Dialog标题分隔符颜色的唯一方法是通过子类化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);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

履行

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

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

}
Run Code Online (Sandbox Code Playgroud)

履行

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

结果

例

  • 就像未来的用户可能遇到的那样; 出于某种原因,当我只使用通用的Dialog时,我必须使用"title"作为我的标识符名称而不是"alertTitle". (4认同)
  • 真的很棒的答案.但它不再适用于appcompat AlertDialog.有谁知道如何用android.support.v7.app.AlertDialog获得标题?我已经尝试了很多,但我总是"无效":-( (4认同)