原文地址:http://www.jianshu.com/p/6caffdbcd5db
前言
谷歌发布了 Material Design 设计之后,很多 Material 风格的控件也随之加入到了 V7 兼容包中.
Android Support Library v22.1 中开始提供了 Material 风格的 Dialog 控件 。
这为开发者提供了很好的支持,省去了使用开源库或自己设计的烦恼。
下面我们来看看如何使用 Material 风格的 Dialog 。
兼容的 AlertDialog
拥有Material风格的Dialog控件在下列类:
android.support.v7.app.AlertDialog所以想要使用此风格的对话框,需要在Module的build.gradle中导入
dependencies { compile 'com.android.support:appcompat-v7:23.1.1' }这个 V7 包中的 AlertDialog 在 Android 2.1 以上可以提供兼容性的 Material 风格 Dialog 。
也就是说,使用这个包中的 AlertDialog 的话,从2.1到6.0都是 Material风格的 Dialog 。
当使用这个包中的 AlertDialog 时:
(下图左边为4.4,右边为5.1)
非兼容的 AlertDialog
如果直接使用
android.app.AlertDialog使用这个包中的 AlertDialog 的话 在 Android 5.0 以下就是原始风格, 5.0 以上为 Material 风格。
我们来看看对比图:
(下图左边为4.4,右边为5.1)
实例
在选择导入包的时候决定了这个是否兼容低版本,因为 Android 碎片化的原因,推荐使用V7包中的AlertDialog达到高低版本统一样式的效果。 这里用一个简单的Demo样式创建一个Dialog并显示的过程。
/** * 这是兼容的 AlertDialog */ private void showDialog() { /* 这里使用了 android.support.v7.app.AlertDialog.Builder 可以直接在头部写 import android.support.v7.app.AlertDialog 那么下面就可以写成 AlertDialog.Builder */ android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(this); builder.setTitle("Material Design Dialog"); builder.setMessage("这是 android.support.v7.app.AlertDialog 中的样式"); builder.setNegativeButton("取消", null); builder.setPositiveButton("确定", null); builder.show(); }常用方法的解释
通过Builder得到对象之后可以进行各种属性的设置,来看看都有哪些属性可供调整。
show
进行显示Dialog。如果在未设置其他属性的情况下直接使用show,会显示一个隐形Dialog,不过背景色会被透明黑色覆盖。
setTitle
设置Dialog的标题,使用一串文本或者。如:
builder.setTitle("这里是Title");setMessage
设置Dialog的内容,使用一串文本。如:
builder.setMessage("这里是Message");setNegativeButton
设置否定按钮,第一个参数为按钮文本,第二个参数为按钮被点击的监听器类。如:
builder.setNegativeButton("取消", null);setPositiveButton
设置肯定按钮,第一个参数为按钮文本,第二个参数为按钮被点击的监听器类。如:
builder.setPositiveButton("确定", null);谷歌使用这两个方法来定义肯定和否定按钮,一定程度上是为了统一肯定和否定按钮所在的方向,肯定按钮统一在右边,因为通常是右手操作,否定按钮统一在左边。这样对用户来说是比较统一的体验。
以上就是最为常用的几个方法 , 接着往下看一些其他方法。
setNeutralButton
设置中性按钮,第一个参数为按钮文本,第二个参数为按钮被点击的监听器类,这个按钮默认被放置到Dialog的左下角。如:
builder.setNeutralButton("中性", null);setView
设置一个自定义的View放置到message的下方,可以是一个View对象,也可以是一个view资源ID。 如:
builder.setView(new EditText(this));setView(可设置边距)
对于使用View对象的参数,还可以设置他的边距。 如:
builder.setView(new EditText(this),20,20,20,20);;setMultiChoiceItems
第一个参数为列表项的文本数组,第二个参数为列表项默认情况下的选中状态,为空则都不选中,第三个是列表项的监听器类。在使用这个方法的时候如果同时使用了setMessage方法会导致此方法无效,而只显示setMessage的内容。 如:
builder.setMultiChoiceItems(new String[]{"233333", "hahahaha"}, null, null);setItems
第一个参数为列表项的文本数组,第二个参数为列表项的监听器类。在使用这个方法的时候如果同时使用了setMessage方法会导致此方法无效,而只显示setMessage的内容。 如:
builder.setItems(new String[]{"Item1", "Item2"}, null);setIco
设置Dialog的图标,可以使用资源文件 ID,也可以使用 Drawable 对象。 如:
builder.setIcon(R.mipmap.ic_launcher);自定义Dialog
有时候自带的各种方法并不能满足我们的Dialog的设计需求,这时候我们可以自己写一个 xml 设计符合需求的Dialog。(仅为设计Dialog的Message部分,并不是Dialog整体)
创建一个名为dialog的XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/dialog" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <TextView android:layout_margin="8dp" android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="你的银行密码:" /> <EditText android:layout_margin="8dp" android:id="@+id/et" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>创建View对象与XML关联
LayoutInflater inflater = getLayoutInflater(); View dialog = inflater.inflate(R.layout.dialog,(ViewGroup) findViewById(R.id.dialog)); EditText editText = (EditText) dialog.findViewById(R.id.et);将View设置到Dialog中
AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("这里是Title"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, editText.getText().toString(), Toast.LENGTH_SHORT).show(); } }); builder.setView(dialog); builder.setIcon(R.mipmap.ic_launcher); builder.show();
本文介绍了如何使用 Android Support Library v22.1 中提供的 Material Design 风格的 AlertDialog 控件,包括其基本用法、常见方法解释及自定义示例。














7124

被折叠的 条评论
为什么被折叠?



