我们知道,在很多时候,我们都不用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. Prison 监狱

    [题目描述]Caima 王国中有一个奇怪的监狱,这个监狱一共有 P 个牢房,这些牢房一字排开,第 i 个仅挨着第 i+1 个(最后一个除外).现在正好牢房是满的.上级下发了一个释放名单,要求每天释放名 ...

  2. [USACO14DEC]后卫马克Guard Mark

    题目描述 FJ将飞盘抛向身高为H(1 <= H <= 1,000,000,000)的Mark,但是Mark 被N(2 <= N <= 20)头牛包围.牛们可以叠成一个牛塔,如果 ...

  3. [USACO16OPEN]关闭农场Closing the Farm_Silver

    题目描述 FJ和他的奶牛们正在计划离开小镇做一次长的旅行,同时FJ想临时地关掉他的农场以节省一些金钱. 这个农场一共有被用M条双向道路连接的N个谷仓(1<=N,M<=3000).为了关闭整 ...

  4. 2015多校联赛 ——HDU5288(数学)

    OO’s Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  5. AR8033 1000M模式下ping包丢包率过大分析与解决

    1 现象 近期对一款基于QCA方案.有线Phy为AR8033.WiFi双频且支持iEEE802.11AC的WLAN产品进行了深度验证,发现有线口同部分PC机直连时,WiFi终端ping 该PC机时总是 ...

  6. JS按照指定的周期来调用函数方法

    setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式. setInterval() 方法会不停地调用函数,直到 clearInterval() 被调用或窗口被关闭.由 s ...

  7. Python Django系统

    本节内容 路由系统,视图函数,模板引擎,ORM操作 FBV和CBV ORM操作补充 Cookie和Session Ajax入门 1.  Django基本内容整理 1.1 路由系统 Django中路由系 ...

  8. NGUI制作可滚动的文本框(摘,如有侵权,联系删除)

    NGUI制作可滚动的文本框 1.首先创建一个UI Root 2.选择UI Root右键 Create 选择Scoll View创建. 3.Scroll view的大小是可以调节的,调节为你需要的适当大 ...

  9. input type="tel" 数字输入框显示圆点

    最近开发中遇到一个这样的需求,要求input输入框在手机端出现数字键盘的同时显示圆点,试过各种方法都不太理想, 最终经过查阅大量资料后,终于实现了需求. ●我们一般的密码输入框是这样的: <in ...

  10. tar包和jar包和war包的区别?

    tar:tar是*nix下的打包工具,生成的包通常也用tar作为扩展名,其实tar只是负责打包,不一定有压缩,事实上可以压缩,也可以不压缩,通常你看到xxxx.tar.gz,就表示这个tar包是压缩的 ...