android 对话框 美化,Android修改Dialog样式

Android 对话框支持自定义标题,内容,按钮和点击事件,基本上可以满足我们日常的使用。

但有时候我们想要修改对话框的文字,按钮颜色等,系统并没有提供对应的方法,正常情况下只能自定义布局。

接下来通过源码解析介绍几种修改 Dialog样式的方法。

一、Dialog源码解析

1.1 new AlertDialog.Builder(this).create()

protected AlertDialog(@NonNull Context context, @StyleRes int themeResId) {

super(context, resolveDialogTheme(context, themeResId));

//创建AlertController,是Dialog布局相关代码

mAlert = new AlertController(getContext(), this, getWindow());

}

@NonNull

public AlertDialog create() {

// We can't use Dialog's 3-arg constructor with the createThemeContextWrapper param,

// so we always have to re-set the theme

final AlertDialog dialog = new AlertDialog(P.mContext, mTheme);

P.apply(dialog.mAlert);

dialog.setCancelable(P.mCancelable);

if (P.mCancelable) {

dialog.setCanceledOnTouchOutside(true);

}

dialog.setOnCancelListener(P.mOnCancelListener);

dialog.setOnDismissListener(P.mOnDismissListener);

if (P.mOnKeyListener != null) {

dialog.setOnKeyListener(P.mOnKeyListener);

}

return dialog;

}

public void apply(AlertController dialog) {

if (mCustomTitleView != null) {

dialog.setCustomTitle(mCustomTitleView);

} else {

if (mTitle != null) {

dialog.setTitle(mTitle);

}

if (mIcon != null) {

dialog.setIcon(mIcon);

}

if (mIconId != 0) {

dialog.setIcon(mIconId);

}

..........

AlertDialog 构造函数中会创建 AlertController,用来控制对话框的布局

P.apply(dialog.mAlert); 将用户自定义的配置赋值给 AlertController

1.2 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);<

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值