自定义 Dialog

来源:互联网 发布:编程导论 pdf 编辑:程序博客网 时间:2024/06/07 10:55

Dialog 在不同的版本具有不同的风格

2.x Dialog

2.x 版本的 Dialog

4.x Dialog

4.x 版本的 Dialog

MD 风格 Dialog

5.0 以上版本的 Dialog,也就是 Material Design 风格的 Dialog

那么能不能在所有的版本中都是用相同风格的 Dialog 呢?当然可以,只要使用 support.v7 包中的 Dialog,就能在 5.0 以下的版本中使用 Material Design 风格的 Dialog。类路径是:

android.support.v7.app.AlertDialog


可是有时候系统的 Dialog 并不能满足我们的需求,这时候就需要对 Dialog 进行自定义。
好在自定义 Dialog 比自定义 View 要简单很多。

自定义 Dialog ,我选择使用的是 Dialog 而不是 AlertDialog ,因为 AlertDialog 的创建比较复杂,而且 AlertDialog 其实就是对 Dialog 的自定义。

让我们开始吧。

首先定义 Dialog 中的布局,叫做 dialog_layout.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:background="@drawable/dialog_background">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:layout_margin="20dp"        android:text="自定义对话框"        android:textSize="15sp"        android:textStyle="bold"/>    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="确定"/></LinearLayout>

布局很简单,只有一个 TextView 跟一个 Button

现在再来编写 Dialog 的背景。在 drawable 目录下新建 dialog_background.xml

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">    <corners android:radius="10dp"/>    <solid android:color="@android:color/holo_blue_light"/></shape>

我们把背景设置成了圆角,背景色为蓝色

接下来就是在 MainActivity 中创建 Dialog

Dialog dialog = new Dialog(this);dialog.setContentView(R.layout.dialog_layout);

运行一下看看吧:

Dialog 的标题区域跟背景

从图中可以看出,顶部有一块白色的区域,这就是 Dialog 的标题区域,我们要把它去掉。

去除 Dialog 的标题

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

注意: 如果使用的是 android.support.v7.app.AppCompatDialog,则应该调用 supportRequestWindowFeature()

看到这个方法有没有觉得很熟悉?没错,给 Activity 去除 ActionBar 就是这么做的,所以该方法必须放在 setContentView 之前,否则将会抛出异常。
在 AlertDialog 的源码中也调用了该方法来去除标题。

再来看看效果:

去除了标题的 Dialog

标题已经去掉了,但是四个角还有一些白色的区域,白色的区域其实是 Dialog 默认的背景,因为我们自定义的布局是圆角的,所以把原有的背景给暴露了出来。这里我们同样要把它去掉。

去除 Dialog 的默认背景

dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

其实就是把默认背景设置成透明。

去除了标题跟默认背景

仔细对比去除默认背景后前后的两幅图可以发现,去掉默认背景后也失去了阴影的效果。

到这里其实也就完成了 Dialog 的自定义,的确很简单吧。下面介绍一下如何设置 Dialog 的其他属性。


设置 Dialog 的显示位置

dialog.getWindow().setGravity(int);

默认值为 Gravity.CENTER

设置 Dialog 的大小

dialog.getWindow().setLayout(int, int);

注意:对 Dialog 大小的设置一定要在 setContentView 之后进行,否则不生效

设置 Dialog 弹出后底下的 Activity 不变暗

dialog.getWindow().setDimAmount(flot);

范围从 0.0 ~ 1.0,0 表示不变暗

设置 Dialog 的显示消失动画

dialog.getWindow().setWindowAnimations(int resId);

设置 Dialog 的背景透明度

dialog.getWindow().getAttributes().alpha = float;

范围从 0.0 ~ 1.0 , 1.0 表示完全不透明

其实上述方法就是对 dialog.getWindow().getAttributes() 返回的 WindowManager.LayoutParams 中的相应属性进行赋值。

实现 Dialog 的自定义有两种方式:

  1. 创建出 Dialog 后再对其进行定制
  2. 继承 Dialog,在 onCreate() 中对 Dialog 中进行定制

修改 AlertDialog 的样式

有时候 AlertDialog 已经可以满足我们的需求,我们只是需要对它的样式进行一些定制。

修改 AlertDialog 按钮的颜色

AlertDialog 并没有提供直接的 API 给我们,所以我们只能曲线救国。这里有一篇文章写得很好:
通过源码分析,修改 AlertDialog 按钮的颜色
看完上面的文章后,相信大家还可以自行的修改 AlertDialog 按钮的 TextSize 跟 TextStyle。

其实我们也是可以通过代码来达到目的,毕竟上面的方法还是有点复杂。
AlertDialog 提供了 getButton() 方法,通过该方法拿到 Button 对象之后我们就可以对 Button 的属性进行设置了。只不过这里有一点需要注意:getButton() 必须在 show() 之后调用,否则返回的是 null

@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    createDialog();}private void createDialog() {    mDialog = new AlertDialog.Builder(this).            setPositiveButton("OK", null).            create();    Button okButton = mDialog.getButton(DialogInterface.BUTTON_POSITIVE);    Log.d(TAG, "okButton is null " + (okButton == null));}

在创建出 AlertDialog 之后我们尝试获取了 PositiveButton 并且打印它是否为空

show() 之前调用 getButton 的结果

接下来把 getButton 延迟到 show() 之后

@OnClick(R.id.bt)public void onClick(View v) {    if (mDialog.isShowing()) {        mDialog.dismiss();    } else {        mDialog.show();        Button okButton = mDialog.getButton(DialogInterface.BUTTON_POSITIVE);        Log.d(TAG, "okButton is null " + (okButton == null));    }}

show() 之后调用 getButton

下面再讲讲如何修改 AlertDialog 的背景颜色、Title 跟 Message 的颜色
http://blog.isming.me/2015/08/31/modify-alert-style/

0 0
原创粉丝点击