我们知道,在很多时候,我们都不用Android内置的一些控件,而是自己自定义一些自己想要的控件,这样显得界面更美观。

今天主要是讲自定义AlertDialog和popupWindow的使用,在很多需求中,我们往往需要这样一个功能,就是点击一个按钮或者其它控件,弹出一个对话框,让用户可以在这个对话框中做一些事,比如输入、选择、提示.....等等,那么,这个弹出对话框的功能我们都知道可以用popupWindow和AlertDialog实现,的却,popupWindow被称为万能的,因为它的布局都是我们自己自定义的,任何的Toast、Dialog它都能实现的,但是Dialog也有它的好处,比如一些单选条目、多选条目之类的,利用AlertDialog就很方便了。比如:

如果我们要实现这个效果:

比如,如果想要实现这个效果,很多人应该第一反应就是用popupWindow吧,这是肯定的,当然可以实现,但是细心的同学就会发现,这个对话框弹出后整个屏幕的背景变了,变的暗了一点。所以这就说说自定义AlertDialog和自定义popupWindow的区别吧:

1、自定义popupWindow它如果不把当前界面设置透明度的话,那么整个屏幕的背景在弹出前和弹出后就没有变化了,所以用popupWindow的来实现这个效果的需要自己设置当前界面透明度的变化

2、自定义AlertDialog它就不需要设置背景了,因为它弹出后,整个屏幕的背景会自动变暗,退出后又自动恢复了

所以,今天就来讲讲以仿微信Dialog为功能来学习自定义AlertDialog怎么实现自定义位置和大小和popupWindow怎么实现弹出屏幕背景变半透明、退出时候恢复屏幕:

一、先用AlertDialog来仿这个功能吧,实际上用AlertDialog实现还稍微有一点麻烦,因为要注意的地方还是很多的,而且它所在屏幕上的位置包括自身的边框设置等局限性挺大的,就是能配置的地方比较少,一般就是用它改变它的位置和大小和自定义View。

好了,废话不多说,直接贴代码吧,代码注释的很清楚了:

自定义View布局文件:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:paddingLeft="10dp"
            android:text="请选择"
            android:textColor="#ff2525"
            android:textSize="20sp" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:background="#969696" />

        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:background="@drawable/dialog_sytle"
            android:gravity="left|center_vertical"
            android:padding="10dp"
            android:text="第一个功能"
            android:textColor="#2a2a2a"
            android:textSize="15sp" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:background="#a6a6a6" />

        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:background="@drawable/dialog_sytle"
            android:gravity="left|center_vertical"
            android:padding="10dp"
            android:text="第二个功能"
            android:textColor="#2a2a2a"
            android:textSize="15sp" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:background="#a6a6a6" />

        <Button
            android:id="@+id/button3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:background="@drawable/dialog_sytle"
            android:gravity="left|center_vertical"
            android:padding="10dp"
            android:text="第三个功能"
            android:textColor="#2a2a2a"
            android:textSize="15sp" />
    </LinearLayout>

</LinearLayout>

自定义AlertDialog的实现代码:

private void showDialog(){
		AlertDialog dialog = new AlertDialog.Builder(this).create();//创建一个AlertDialog对象
		View view = getLayoutInflater().inflate(R.layout.dialog, null);//自定义布局
		dialog.setView(view, 0, 0, 0, 0);//把自定义的布局设置到dialog中,注意,布局设置一定要在show之前。从第二个参数分别填充内容与边框之间左、上、右、下、的像素
		dialog.show();//一定要先show出来再设置dialog的参数,不然就不会改变dialog的大小了
		int width = getWindowManager().getDefaultDisplay().getWidth();//得到当前显示设备的宽度,单位是像素
		LayoutParams params = dialog.getWindow().getAttributes();//得到这个dialog界面的参数对象
		params.width = width-(width/6);//设置dialog的界面宽度
		params.height =  LayoutParams.WRAP_CONTENT;//设置dialog高度为包裹内容
		params.gravity = Gravity.CENTER;//设置dialog的重心
		//dialog.getWindow().setLayout(width-(width/6),  LayoutParams.WRAP_CONTENT);//用这个方法设置dialog大小也可以,但是这个方法不能设置重心之类的参数,推荐用Attributes设置
		dialog.getWindow().setAttributes(params);//最后把这个参数对象设置进去,即与dialog绑定

	}

再贴下效果图:

二、我再用PopupWindow来实现这个效果吧,需要注意的一点就是,利用PopupWindow实现时,首先它弹出来的时候我们就要设置当前界面的透明度,然后监听PopupWindow的dismiss消失事件,再PopupWindow消失时候再恢复当前界面的透明度,这样就是实现了弹出时候背景变暗,消失时候背景恢复了,好了,我们开始来实现吧:

xml布局文件为:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:paddingLeft="10dp"
            android:text="请选择"
            android:textColor="#ff2525"
            android:textSize="20sp" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:background="#969696" />

        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:background="@drawable/dialog_sytle"
            android:gravity="left|center_vertical"
            android:padding="10dp"
            android:text="第一个功能"
            android:textColor="#2a2a2a"
            android:textSize="15sp" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:background="#a6a6a6" />

        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:background="@drawable/dialog_sytle"
            android:gravity="left|center_vertical"
            android:padding="10dp"
            android:text="第二个功能"
            android:textColor="#2a2a2a"
            android:textSize="15sp" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:background="#a6a6a6" />

        <Button
            android:id="@+id/button3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:background="@drawable/dialog_sytle"
            android:gravity="left|center_vertical"
            android:padding="10dp"
            android:text="第三个功能"
            android:textColor="#2a2a2a"
            android:textSize="15sp" />
    </LinearLayout>

</LinearLayout>

代码实现方法为:

private void popWindow(){
		LayoutInflater inflater = LayoutInflater.from(this);//获取一个填充器
		View view = inflater.inflate(R.layout.popwindow, null);//填充我们自定义的布局
		Display display = getWindowManager().getDefaultDisplay();//得到当前屏幕的显示器对象
		Point size = new Point();//创建一个Point点对象用来接收屏幕尺寸信息
		display.getSize(size);//Point点对象接收当前设备屏幕尺寸信息
		int width = size.x;//从Point点对象中获取屏幕的宽度(单位像素)
		int height = size.y;//从Point点对象中获取屏幕的高度(单位像素)
		Log.v("zxy", "width="+width+",height="+height);//width=480,height=854可知手机的像素是480x854的
		//创建一个PopupWindow对象,第二个参数是设置宽度的,用刚刚获取到的屏幕宽度乘以2/3,取该屏幕的2/3宽度,从而在任何设备中都可以适配,高度则包裹内容即可,最后一个参数是设置得到焦点
		PopupWindow popWindow = new PopupWindow(view, 2*width/3, LayoutParams.WRAP_CONTENT, true);
		popWindow.setBackgroundDrawable(new BitmapDrawable());//设置PopupWindow的背景为一个空的Drawable对象,如果不设置这个,那么PopupWindow弹出后就无法退出了
		popWindow.setOutsideTouchable(true);//设置是否点击PopupWindow外退出PopupWindow
		WindowManager.LayoutParams params = getWindow().getAttributes();//创建当前界面的一个参数对象
		params.alpha = 0.8f;//设置参数的透明度为0.8,透明度取值为0~1,1为完全不透明,0为完全透明,因为android中默认的屏幕颜色都是纯黑色的,所以如果设置为1,那么背景将都是黑色,设置为0,背景显示我们的当前界面
		getWindow().setAttributes(params);//把该参数对象设置进当前界面中
		popWindow.setOnDismissListener(new OnDismissListener() {//设置PopupWindow退出监听器
			@Override
			public void onDismiss() {//如果PopupWindow消失了,即退出了,那么触发该事件,然后把当前界面的透明度设置为不透明
				WindowManager.LayoutParams params = getWindow().getAttributes();
				params.alpha = 1.0f;//设置为不透明,即恢复原来的界面
				getWindow().setAttributes(params);
			}
		});
		//第一个参数为父View对象,即PopupWindow所在的父控件对象,第二个参数为它的重心,后面两个分别为x轴和y轴的偏移量
		popWindow.showAtLocation(inflater.inflate(R.layout.activity_main, null), Gravity.CENTER, 0, 0);

	}

再来看看效果图:

嘿嘿,是不是后面背景变暗了啊,然后退出PopupWindow时候又会恢复原状的。

总结一下:在实际开发中呢,我建议使用PopupWindow来做的,因为AlertDialog更适合做一些选择按钮等,而一般的对话框则最好用PopupWindow来做,因为PopupWindow的灵活性和可设置性更高,非常的灵活!!!

转载请注明出处---------谢谢!

Android之自定义AlertDialog和PopupWindow实现(仿微信Dialog)的更多相关文章

  1. 【转】android 欢迎界面翻页成效,仿微信第一次登陆介绍翻页界面

    android 欢迎界面翻页效果,仿微信第一次登陆介绍翻页界面 本实例做的相对比较简单主要是对翻页控件的使用,有时候想要做一些功能是主要是先了解下是否有现成的控件可以使用,做起来比较简单不用费太大的劲 ...

  2. Android开发学习之路-PopupWindow和仿QQ左滑删除

    这周作业,要做一个类似QQ的左滑删除效果的ListView,因为不想给每个item都放一个按钮,所以决定用PopupWindow,这里记录一下 先放一下效果图: 先说明一下这里面的问题: ①没有做到像 ...

  3. Android 高级UI设计笔记06:仿微信图片选择器(转载)

    仿微信图片选择器: 一.项目整体分析: 1. Android加载图片的3个目标: (1)尽可能的去避免内存溢出. a. 根据图片的显示大小去压缩图片 b. 使用缓存对我们图片进行管理(LruCache ...

  4. uni-app自定义导航栏按钮|uniapp仿微信顶部导航条

    最近一直在学习uni-app开发,由于uniapp是基于vue.js技术开发的,只要你熟悉vue,基本上很快就能上手了. 在开发中发现uni-app原生导航栏也能实现一些顶部自定义按钮+搜索框,只需在 ...

  5. Android之自定义AlertDialog无法监听控件

    参考:http://www.cnblogs.com/511mr/archive/2011/10/21/2220253.html 要做一个自定义的弹出框,以前都是用一个Activity来实现,总觉得不是 ...

  6. Taro自定义Modal对话框组件|taro仿微信、android弹窗

    基于Taro多端实践TaroPop:自定义模态框|dialog对话框|msg消息框|Toast提示 taro自定义弹出框支持编译到多端H5/小程序/ReactNative,还可以自定义弹窗类型/弹窗样 ...

  7. Android中自定义弹出PopupWindow后返回按键失效的问题解决

    PopupWindow出现之后,默认的是所有的操作都无效的,除了HOME键.而且是可以操作后面的界面的.想要锁定后面的界面,很简单,只需要让PopupWindow是focusable的:popupWi ...

  8. <Android 基础(三十三)> TabHost ~ 仿微信底部菜单

    简介 Container for a tabbed window view. This object holds two children: a set of tab labels that the ...

  9. uni-app聊天室|vue+uniapp仿微信聊天实例|uniapp仿微信App界面

    一.介绍 运用UniApp+Vue+Vuex+swiper+uniPop等技术开发的仿微信原生App聊天室|仿微信聊天界面实例项目uniapp-chatroom,实现了发送图文消息.表情(gif图), ...

随机推荐

  1. 【20170920校内模拟赛】小Z爱学习

    所有题目开启-O2优化,开大栈空间,评测机效率为4亿左右. T1 小 Z 学数学(math) Description ​ 要说小 Z 最不擅长的学科,那一定就是数学了.这不,他最近正在学习加法运算.老 ...

  2. 2015 多校联赛 ——HDU5386(暴力)

    Sample Input 1 3 5 2 2 1 2 3 3 2 1 3 3 3 3 3 3 3 3 3 3 H 2 3 L 2 2 H 3 3 H 1 3 L 2 3   Sample Output ...

  3. [hdu5608]function

    题意:$\sum_{d|n}f(d)=n^{2}-3n+2$,求$\sum_{i=1}^{n}f(i)\mod 10^{9}+7$ , $n \leqslant 10^{9}$ $\left( T \ ...

  4. Python的序列类型——List

    List 列表 List,本质是一个链表,从链表的实现角度来讲,链表的每一个结点都存放着值和指向下一个节点的指针. 因此链表在内存的存储可以是不连续的,它是一种高效的数据结构.因此列表与字符串的区别是 ...

  5. JSP 基本语法

    1 JSP 的由来 servlet产生后,存在很大的问题,为了表现页面的效果,需要输出大量的HTML 语句,表现为一个个字符串,不仅利于开发,也不利于后期的维护,由此产生了JSP.主要用于将Servl ...

  6. xml 和数组的相互转化

    数组转化为xml: function arrtoxml($arr,$dom=0,$item=0){ if (!$dom){ $dom = new DOMDocument("1.0" ...

  7. 笔记13 AOP中After和AfterReturning的区别

    AOP中 @Before @After @AfterThrowing@AfterReturning的执行顺序 public Object invoke(Object proxy, Method met ...

  8. Java Servlet 笔记3

    Servlet 表单数据 很多情况下,需要传递一些信息,从浏览器到 Web 服务器,最终到后台程序.浏览器使用两种方法可将这些信息传递到 Web 服务器,分别为 GET 方法和 POST 方法. 1. ...

  9. P2P技术详解(二):P2P中的NAT穿越(打洞)方案详解

    1.内容概述 P2P即点对点通信,或称为对等联网,与传统的服务器客户端模式(如下图"P2P结构模型"所示)有着明显的区别,在即时通讯方案中应用广泛(比如IM应用中的实时音视频通信. ...

  10. 华科机考:a+b

    时间限制:1秒     空间限制:32768K 题目描述 实现一个加法器,使其能够输出a+b的值. 输入描述: 输入包括两个数a和b,其中a和b的位数不超过1000位. 输出描述: 可能有多组测试数据 ...