Android编程使用android-support-design实现MD风格对话框功能示例

 更新时间:2017年01月20日 11:42:48   作者:books1958  
这篇文章主要介绍了Android编程使用android-support-design实现MD风格对话框功能,涉及Android对话框、视图、布局相关操作技巧,需要的朋友可以参考下

本文实例讲述了Android编程使用android-support-design实现MD风格对话框功能。分享给大家供大家参考,具体如下:

首先上效果图:

 

测试设备为红米Note,系统为Android 4.4.4

说明:

1.在新版的android.support.v7包中,Google提供了一个新的AlertDialog类,即android.support.v7.app.AlertDialog。使用该类中的Builder可以直接创建Material Design风格的对话框,而不需要再借助于第三方库。(即第一张图的效果)

2.遗憾的是,上述第二张图中转圈样式的ProgressBar暂无法使用系统组件。本例中使用的第三方库来自:

compile 'com.github.rahatarmanahmed:circularprogressview:2.4.0'

3.代码不多,并已简单封装为工具类:

package com.sinatj.demo.utils;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import com.sinatj.demo.R;
/**
 * UiUtil.
 * Created by admin on 15-12-22.
 */
public class UiUtil {
  private static AlertDialog showDialog(Context context, String title, String message, View contentView,
     String positiveBtnText, String negativeBtnText,
     DialogInterface.OnClickListener positiveCallback,
     DialogInterface.OnClickListener negativeCallback,
     boolean cancelable) {
    AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.AppCompatAlertDialogStyle);
    builder.setTitle(title == null ? "提示" : title);
    if (message != null) {
      builder.setMessage(message);
    }
    if (contentView != null) {
      builder.setView(contentView);
    }
    if (positiveBtnText != null) {
      builder.setPositiveButton(positiveBtnText, positiveCallback);
    }
    if (negativeBtnText != null) {
      builder.setNegativeButton(negativeBtnText, negativeCallback);
    }
    builder.setCancelable(cancelable);
    return builder.show();
  }
  //普通对话框
  public static AlertDialog showSimpleDialog(Context context, String title, String message,
        String positiveBtnText, String negativeBtnText,
        DialogInterface.OnClickListener positiveCallback,
        DialogInterface.OnClickListener negativeCallback,
        boolean cancelable) {
    return showDialog(context, title, message, null, positiveBtnText, negativeBtnText, positiveCallback, negativeCallback, cancelable);
  }
  //带ProgressBar的对话框
  public static AlertDialog showProgressDialog(Context context, String title, String message,
     String positiveBtnText, String negativeBtnText,
     DialogInterface.OnClickListener positiveCallback,
     DialogInterface.OnClickListener negativeCallback,
     boolean cancelable) {
    View view = LayoutInflater.from(context).inflate(R.layout.circular_progressbar, null);
    if (message != null) {
      final TextView messageTv = (TextView) view.findViewById(R.id.progressbar_msg);
      messageTv.setText(message);
    }
    return showDialog(context, title, null, view, positiveBtnText, negativeBtnText, positiveCallback, negativeCallback, cancelable);
  }
}

4.circular_progressbar布局文件,由一个第三方库提供的ProgressBar和一个TextView组成:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:orientation="horizontal"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:padding="20dp">
  <com.github.rahatarmanahmed.cpv.CircularProgressView
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:orientation="vertical"
    app:cpv_animAutostart="true"
    app:cpv_indeterminate="true" />
  <TextView
    android:id="@+id/progressbar_msg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_gravity="center_vertical"
    android:textSize="16sp"
    android:textColor="#111111"
    android:text="@string/main_waiting"/>
</LinearLayout>

5.AppCompatAlertDialogStyle为对话框的样式,可指定文字颜色、按钮颜色、背景色等。(本例中使用的时默认值)

<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <!--对话框按钮文字颜色-->
    <item name="colorAccent">#FFCC00</item>
    <!--对话框内容文字颜色-->
    <item name="android:textColorPrimary">#FFFFFF</item>
    <!--对话框背景色-->
    <item name="android:background">#5fa3d0</item>
</style>

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android编程之activity操作技巧总结》、《Android资源操作技巧汇总》、《Android视图View技巧总结》、《Android布局layout技巧总结》、《Android开发入门与进阶教程》、《Android视图View技巧总结》及《Android控件用法总结

希望本文所述对大家Android程序设计有所帮助。

相关文章

  • Android动画之3D翻转效果实现函数分析

    Android动画之3D翻转效果实现函数分析

    Android中的翻转动画效果的实现,Android中并没有提供直接做3D翻转的动画,所以关于3D翻转的动画效果需要我们自己实现,那么我们首先来分析一下Animation 和 Transformation,感兴趣的朋友可以了解下啊
    2013-01-01
  • Android实现下载m3u8视频文件问题解决

    Android实现下载m3u8视频文件问题解决

    这篇文章主要介绍了Android实现下载m3u8视频文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2023-01-01
  • Android TimePicker 直接输入的问题解决方案

    Android TimePicker 直接输入的问题解决方案

    这篇文章主要介绍了Android TimePicker 直接输入的问题解决方案的相关资料,需要的朋友可以参考下
    2017-04-04
  • android中Webview实现截屏三种方式小结

    android中Webview实现截屏三种方式小结

    本篇文章主要介绍了android Webview实现截屏,主要详解了3种方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-03-03
  • 详解四种主要的Android依赖管理方式

    详解四种主要的Android依赖管理方式

    Android应用开发涉及大量的依赖库和第三方组件,因此有效地管理这些依赖关系至关重要,本文将介绍四种主要的Android依赖管理方式,分析它们的优点、缺点以及最佳实践,需要的朋友可以参考下
    2023-09-09
  • ViewPager顶部导航栏联动效果(标题栏条目多)

    ViewPager顶部导航栏联动效果(标题栏条目多)

    这篇文章主要介绍了ViewPager顶部导航栏联动效果,代码简单易懂,感兴趣的朋友参考下吧
    2016-08-08
  • Android第三方HTTP网络支持包OkHttp的基础使用教程

    Android第三方HTTP网络支持包OkHttp的基础使用教程

    在GitHub上开源的安卓HTTP编程包OkHttp正在积累着越来越高的人气,这里我们就来看一下这款Android第三方HTTP网络支持包OkHttp的基础使用教程:
    2016-07-07
  • Android MVP模式面向接口写法

    Android MVP模式面向接口写法

    这篇文章主要介绍了Android MVP模式面向接口写法,MVP模式也出来好几年了,很成熟所以也导致写法有很多种,google提供了多种mvp模式,但我今天只讲解最简单的面向接口,需要详细了解可以参考下文
    2023-05-05
  • Android注解使用之ButterKnife 8.0详解

    Android注解使用之ButterKnife 8.0详解

    这篇文章主要为大家详细介绍了Android注解使用之ButterKnife 8.0的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • Android startActivityForResult()代替方案示例

    Android startActivityForResult()代替方案示例

    这篇文章主要为大家介绍了Android startActivityForResult()代替方案示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08

最新评论