Android-用style修改AlertDialog按钮文字颜色

android系统为开发者提供了AlertDialog,用于快捷创建dialog。

Android-用style修改AlertDialog按钮文字颜色_第1张图片

用Builder模式创建,可以轻松设置title,message,取消,确定按钮点击事件等。但是本人实际项目用的比较少,因为跟UI设计的样式不太一样,按钮文字颜色不太好修改。经过一番查找,找到了修改按钮文字颜色的方法。

AlertDialog最底层的构造方法:
/**
     * Construct an AlertDialog that uses an explicit theme.  The actual style
     * that an AlertDialog uses is a private implementation, however you can
     * here supply either the name of an attribute in the theme from which
     * to get the dialog's style (such as {@link R.attr#alertDialogTheme}.
     */
    protected AlertDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, resolveDialogTheme(context, themeResId));
        mAlert = new AlertController(getContext(), this, getWindow());
    }
AlertController控制的布局。
public AlertController(Context context, AppCompatDialog di, Window window) {
        mContext = context;
        mDialog = di;
        mWindow = window;
        mHandler = new ButtonHandler(di);

        final TypedArray a = context.obtainStyledAttributes(null, R.styleable.AlertDialog,
                R.attr.alertDialogStyle, 0);

        mAlertDialogLayout = a.getResourceId(R.styleable.AlertDialog_android_layout, 0);
        mButtonPanelSideLayout = a.getResourceId(R.styleable.AlertDialog_buttonPanelSideLayout, 0);

        mListLayout = a.getResourceId(R.styleable.AlertDialog_listLayout, 0);
        mMultiChoiceItemLayout = a.getResourceId(R.styleable.AlertDialog_multiChoiceItemLayout, 0);
        mSingleChoiceItemLayout = a
                .getResourceId(R.styleable.AlertDialog_singleChoiceItemLayout, 0);
        mListItemLayout = a.getResourceId(R.styleable.AlertDialog_listItemLayout, 0);
        mShowTitle = a.getBoolean(R.styleable.AlertDialog_showTitle, true);

        a.recycle();

        /* We use a custom title so never request a window title */
        di.supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
    }
可以看到各种自定义属性,是用R.styleable.AlertDialog获取的。打开android系统values搜索AlertDialog:

Android-用style修改AlertDialog按钮文字颜色_第2张图片

可以看到有一个style的属性,与AlertDialog的R.styleable.AlertDialog自定义属性是对应的,说明可以控制UI样式。
		@layout/abc_alert_dialog_material
        @layout/abc_select_dialog_material
        @layout/select_dialog_item_material
        @layout/select_dialog_multichoice_material
        @layout/select_dialog_singlechoice_material
打开@layout/abc_alert_dialog_material:





    

    

        

        

            

                

                

                
            
        

        

    

    

        
    

    


最下面include了一个layout点进去:





    

        
button2和button1就是AlertDialog的取消和确定按钮。样式分别用了buttonBarNegativeButtonStyle和buttonBarPositiveButtonStyle两个style。搜索这两个style发现:
@style/Widget.AppCompat.ButtonBar
	@style/Widget.AppCompat.Button.ButtonBar.AlertDialog
        ?attr/buttonBarButtonStyle
        ?attr/buttonBarButtonStyle
        ?attr/buttonBarButtonStyle
那么我们只需要在自己的主题重新定buttonBarNegativeButtonStyle和buttonBarPositiveButtonStyle两个style就可以修改按钮样式了。
看下效果:

Android-用style修改AlertDialog按钮文字颜色_第3张图片

我修改的样式:


    
    

    
    

    
    





AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("你确定要把这5块钱捐给红十字会吗?")
                .setTitle("标题")
                .setNegativeButton("取消",null)
                .setPositiveButton("确定",null)
                .show();

你可能感兴趣的:(Android)