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);
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);
mButtonIconDimen = a.getDimensionPixelSize(R.styleable.AlertDialog_buttonIconDimen, 0);
a.recycle();
/* We use a custom title so never request a window title */
di.supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
}
R.attr.alertDialogStyle 是 对话框的默认样式,
@style/AlertDialog.AppCompat
@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
@dimen/abc_alert_dialog_button_dimen
上述代码可以看出,abc_alert_dialog_material 就是dialog的默认布局。
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parentPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="start|left|top"
android:orientation="vertical">
android:id="@+id/contentPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp">
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_gravity="top"
android:background="?attr/colorControlHighlight"
android:visibility="gone"/>
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:id="@+id/textSpacerNoTitle"
android:layout_width="match_parent"
android:layout_height="@dimen/abc_dialog_padding_top_material"
android:visibility="gone"/>
android:id="@android:id/message"
style="@style/TextAppearance.AppCompat.Subhead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="?attr/dialogPreferredPadding"
android:paddingRight="?attr/dialogPreferredPadding"/>
android:id="@+id/textSpacerNoButtons"
android:layout_width="match_parent"
android:layout_height="@dimen/abc_dialog_padding_top_material"
android:visibility="gone"/>
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_gravity="bottom"
android:background="?attr/colorControlHighlight"
android:visibility="gone"/>
android:id="@+id/customPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp">
android:id="@+id/custom"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
标题布局:
android:id="@+id/topPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:id="@+id/title_template"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|start|left"
android:orientation="horizontal"
android:paddingLeft="?attr/dialogPreferredPadding"
android:paddingRight="?attr/dialogPreferredPadding"
android:paddingTop="@dimen/abc_dialog_padding_top_material">
android:id="@android:id/icon"
android:layout_width="32dip"
android:layout_height="32dip"
android:layout_marginEnd="8dip"
android:layout_marginRight="8dip"
android:scaleType="fitCenter"
android:src="@null"/>
android:id="@+id/alertTitle"
style="?android:attr/windowTitleStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:ellipsize="end"
android:singleLine="true"
android:textAlignment="viewStart"/>
android:id="@+id/titleDividerNoCustom"
android:layout_width="match_parent"
android:layout_height="@dimen/abc_dialog_title_divider_material"
android:visibility="gone"/>
按钮布局:
android:id="@+id/buttonPanel"
style="?attr/buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollIndicators="top|bottom">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:layoutDirection="locale"
android:orientation="horizontal"
android:paddingBottom="4dp"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:paddingTop="4dp">
android:id="@android:id/button3"
style="?attr/buttonBarNeutralButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
android:id="@+id/spacer"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"
android:visibility="invisible"/>
android:id="@android:id/button2"
style="?attr/buttonBarNegativeButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
android:id="@android:id/button1"
style="?attr/buttonBarPositiveButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
二、修改Dialog样式
2.1 通过findViewById
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(msg);
builder.setPositiveButton(getString(R.string.yes), null);
AlertDialog dialog = builder.create();
dialog.show();
//直接通过id找到对应的控件
Button button = dialog.findViewById(android.R.id.button1);
//或者通过getButton方法也可以获取到
Button button2 = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
这种修改方式必须在 show() 之后调用,否则会出现空指针异常。这个是因为,执行 show() 方法的时候,dialog才会初始化布局,具体源码可以查看 Dialog 的 onCreate 方法。
2.2 自定义style
通过上面源码可以发现,Dialog三个按钮的样式如下:
buttonBarNeutralButtonStyle
buttonBarNegativeButtonStyle
buttonBarPositiveButtonStyle
android:id="@android:id/button3"
style="?attr/buttonBarNeutralButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
android:id="@+id/spacer"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"
android:visibility="invisible"/>
android:id="@android:id/button2"
style="?attr/buttonBarNegativeButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
android:id="@android:id/button1"
style="?attr/buttonBarPositiveButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
自定义样式替换上述 style即可达到修改效果。
在style.xml添加如下代码:
@color/test1
@color/test2
@style/accessPositiveBtnStyle
@style/accessNegativeBtnStyle
具体使用:
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.testDialogTheme);
builder.setMessage("Test");
builder.setCancelable(false);
builder.setPositiveButton("确认", null);
builder.setNegativeButton("取消", null);
Dialog dialog = builder.create();
dialog.show();
android 对话框 美化,Android修改Dialog样式相关推荐
- android listview 美化,Android界面美化 -- 自定义ListView分割线
ListView默认的分割线 ListView会在item之间添加一个默认的分割线.在XML中添加一个ListView,其对应的属性如下. 可以看到ListView默认样式中设置了一个Divider, ...
- android 对话框大全,Android 对话框(Dialog)大全
Activities提供了一种方便管理的建立.保存.回复的对话框机制,例如onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog(i ...
- android对话框心得,Android ProgressDialog使用总结
ProgressDialog 继承自AlertDialog,AlertDialog继承自Dialog,实现DialogInterface接口. ProgressDialog的创建方式有两种,一种是ne ...
- android 对话框 图片,android – AlertDialog按钮的图像
由于不推荐使用onPrepareDialog,因此您可以使用onShowListener. 你也应该设置Drawable边界,或者它将被放置在最左边. 下面的代码输出 public class MyD ...
- android edittext 美化,android EditText的美化
使用alloctor模板来实现string类 虽然以前做过更复杂的各种数据结构,不过那只是在看完c++prime7章后做的,没有考虑到类的拷贝体现出来是类值还是类指针,于是写了一些半成品类,不过那些主 ...
- android对话框知识点,Android ProgressDialog知识要点
释放双眼,带上耳机,听听看~! 当自己做一些耗时操作时,希望给用户一些提示信息,告诉用户正在进行耗时操作,这时就可以用到ProgressDialog. 1.新建一个全局变量ProgressDialog ...
- android 对话框白色样式,Android 对话框(Dialog)样式大全以及简单实现
下面是几种对话框的效果 图一: 图二: 图三: 图四: 图五: 图六: 图七: 图1效果:该效果是当按返回按钮时弹出一个提示,来确保无误操作,采用常见的对话框样式. 代码: 创建对话框方法dialog ...
- Android 对话框(Dialog)大全
Activities提供了一种方便管理的创建.保存.回复的对话框机制,例如onCreateDialog(int),onPrepareDialog(int,Dialog),showDialog(int) ...
- Android对话框dialog大全(代码直接可用)
Activities提供了一种方便管理的创建.保存.回复的对话框机制,例如 onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog( ...
最新文章
- 大厂面试录取通过率不到3%,我真是太太太难了......
- 什么样的网站结构备受搜索引擎喜爱?
- 微软.Net开发中的多线程编程总结
- 生命html文档,Web前端第一季(HTML)
- Android 常用设计模式——观察者模式,单例模式,Builder模式,原型模式,命令模式
- slow log php,善用php-fpm的慢执行日志slow log,分析php性能问题
- (王道408考研操作系统)第四章文件管理-第一节1:文件管理初识
- HTTPs 相关的东西
- 讯飞庭审语音识别系统亮相最高人民法院工作报告
- 微信小程序常用样式,特效,方法
- 10、Lctech Pi(F1C200S)驱动电阻屏触摸芯片ns2009(ts2007),buildroot配置tslib(CherryPi,Mangopi,F1C100S)
- php gtk 中文手册,PHP-GTK
- sqlserver 企业版下载地址
- PostgreSQL下载和安装教程
- linux查看m2网卡驱动,求助 华硕P5M2-E的千兆网卡驱动如何安装?
- 支付宝回调验证失败问题
- NET 模拟Htpp请求
- Android手机扫描识别银行卡技术
- 有史以来影响世界的颠覆性技术 | 未来的高附加值颠覆性技术产业
- [渝粤教育] 西南科技大学 法律文书写作 在线考试复习资料