美文网首页
05_Android Material Design Alert

05_Android Material Design Alert

作者: Android_小生 | 来源:发表于2017-07-31 10:41 被阅读427次

1.前言


Android5.0 之后,很多控件都具有了 Material Design 的效果,看起来更炫,更好看。这些控件大多数都放在 v7 包里面。

我们只需在 gradle 文件中添加对应的依赖即可使用这些 v7 包的控件。这些控件在 Android5.0 以下也提供了很好的兼容性。所以我们在选择控件的导入包的时候,尽量选择 v7 包的,因为它向下有很好的兼容性。

compile 'com.android.support:appcompat-v7:26.+'

2. AlertDialog 使用


2.1 简单使用

效果图.jpg

代码示例如下:

private void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(" Meterial Design AlertDialog")
                .setMessage("我是 v7 包里的 AlertDialog")
                .setNegativeButton("取消", null)
                .setPositiveButton("确定", null)
                .show();
    }

2.2 其它设置

2.2.1 在 Message 位置设置 items 条目

setItems() 方法

效果图.jpg

代码示例如下:

private void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(" Meterial Design AlertDialog")
               builder.setTitle(" Meterial Design AlertDialog")
//                .setMessage("我是 v7 包里的 AlertDialog")
                .setItems(new String[]{"战狼1", "战狼2", "战狼3"}, null)
                .setNegativeButton("取消", null)
                .setPositiveButton("确定", null)
                .show();
    }

需要注意的是,对 Message 位置设置的 setMessage() 方法应去掉,否则下面的 item 样式不显示。下面的设置也是如此。

2.2.2 在 Message 位置设置多选 items 条目

setMultiChoiceItems() 方法

效果图.jpg

代码示例如下:

private void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(" Meterial Design AlertDialog")
               builder.setTitle(" Meterial Design AlertDialog")
//                .setMessage("我是 v7 包里的 AlertDialog")
                .setMultiChoiceItems(new String[]{"战狼1", "战狼2", "战狼3"}, null, null)
                .setNegativeButton("取消", null)
                .setPositiveButton("确定", null)
                .show();
    }

在 setMultiChoiceItems() 方法中,第二个参数 boolean[] checkedItems 为所有 items 的选中状态,第三个参数就是对多个 items 的选中监听。

2.2.3 自定义 dialog 样式

setView() 方法

这里的自定义并不是对于整个的 AlertDialog 控件,而是控件中的 message 位置进行重新设置。

我们可以在代码中动态设置,也可以在 xml 文件中创建自己的自定义控件内容。

效果图.jpg

下面是具体代码的实现步骤

dialog_message.xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <NumberPicker
        android:id="@+id/num_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:padding="10dp"
        android:text="年"/>

    <NumberPicker
        android:id="@+id/num_two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:padding="10dp"
        android:text="月"/>

</LinearLayout>

创建 View 对象,将 xml 文件与 View 对象关联起来

        LayoutInflater inflater = getLayoutInflater();
        View  numView = inflater.inflate(R.layout.dialog_message, null);
        NumberPicker numberPicker1 = numView.findViewById(R.id.num_one);
        NumberPicker numberPicker2 = numView.findViewById(R.id.num_two);

        numberPicker1.setMaxValue(2020);
        numberPicker1.setMinValue(2011);
        numberPicker1.setValue(2017);

        numberPicker2.setMaxValue(12);
        numberPicker2.setMinValue(1);
        numberPicker2.setValue(7);

将 View 设置到 AlertDialog 中

...
AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(" Meterial Design AlertDialog")
                .setNegativeButton("取消", null)
                .setPositiveButton("确定", null)
                .setView(numView)
                .show();

还有一些其它设置,大家可以根据自己的需要,自己实践一下。

参考文章
[Android] Material 风格的 Dialog 的使用

相关文章

网友评论

      本文标题:05_Android Material Design Alert

      本文链接:https://www.haomeiwen.com/subject/bjyllxtx.html