【问题标题】:How to change Appcompat dialog title and title divider color?如何更改 Appcompat 对话框标题和标题分隔线颜色?
【发布时间】:2014-04-11 22:53:29
【问题描述】:

有什么方法可以更改 Appcompat 对话框标题和标题分隔线颜色?我不想使用全息浅蓝色。

我创建了这个 link,但它是为了全息光而不能与 appcompat 一起使用。

提前致谢。

【问题讨论】:

    标签: android dialog android-appcompat


    【解决方案1】:

    更改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);
            }
        }
    
    }
    

    实施

    @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 我使用DialogFragmentAlertDialog.Builder 使用示例进行了编辑。
    • 提醒未来可能遇到此问题的用户;出于某种原因,当我只使用一个通用对话框时,我必须使用“title”作为我的标识符名称,而不是“alertTitle”。
    • 非常好的答案。但它不再适用于 appcompat AlertDialog。有谁知道我如何使用 android.support.v7.app.AlertDialog 获得标题?我已经尝试了很多,但我总是“空”:-(
    • title=null and titleDivider=null // 编译 'com.android.support:appcompat-v7:23.1.1'
    猜你喜欢
    • 2013-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 2013-11-23
    • 1970-01-01
    • 2015-04-05
    • 2014-02-15
    相关资源
    最近更新 更多