Android之自定义AlertDialog和PopupWindow实现(仿微信Dialog)
我们知道,在很多时候,我们都不用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)的更多相关文章
- 【转】android 欢迎界面翻页成效,仿微信第一次登陆介绍翻页界面
android 欢迎界面翻页效果,仿微信第一次登陆介绍翻页界面 本实例做的相对比较简单主要是对翻页控件的使用,有时候想要做一些功能是主要是先了解下是否有现成的控件可以使用,做起来比较简单不用费太大的劲 ...
- Android开发学习之路-PopupWindow和仿QQ左滑删除
这周作业,要做一个类似QQ的左滑删除效果的ListView,因为不想给每个item都放一个按钮,所以决定用PopupWindow,这里记录一下 先放一下效果图: 先说明一下这里面的问题: ①没有做到像 ...
- Android 高级UI设计笔记06:仿微信图片选择器(转载)
仿微信图片选择器: 一.项目整体分析: 1. Android加载图片的3个目标: (1)尽可能的去避免内存溢出. a. 根据图片的显示大小去压缩图片 b. 使用缓存对我们图片进行管理(LruCache ...
- uni-app自定义导航栏按钮|uniapp仿微信顶部导航条
最近一直在学习uni-app开发,由于uniapp是基于vue.js技术开发的,只要你熟悉vue,基本上很快就能上手了. 在开发中发现uni-app原生导航栏也能实现一些顶部自定义按钮+搜索框,只需在 ...
- Android之自定义AlertDialog无法监听控件
参考:http://www.cnblogs.com/511mr/archive/2011/10/21/2220253.html 要做一个自定义的弹出框,以前都是用一个Activity来实现,总觉得不是 ...
- Taro自定义Modal对话框组件|taro仿微信、android弹窗
基于Taro多端实践TaroPop:自定义模态框|dialog对话框|msg消息框|Toast提示 taro自定义弹出框支持编译到多端H5/小程序/ReactNative,还可以自定义弹窗类型/弹窗样 ...
- Android中自定义弹出PopupWindow后返回按键失效的问题解决
PopupWindow出现之后,默认的是所有的操作都无效的,除了HOME键.而且是可以操作后面的界面的.想要锁定后面的界面,很简单,只需要让PopupWindow是focusable的:popupWi ...
- <Android 基础(三十三)> TabHost ~ 仿微信底部菜单
简介 Container for a tabbed window view. This object holds two children: a set of tab labels that the ...
- uni-app聊天室|vue+uniapp仿微信聊天实例|uniapp仿微信App界面
一.介绍 运用UniApp+Vue+Vuex+swiper+uniPop等技术开发的仿微信原生App聊天室|仿微信聊天界面实例项目uniapp-chatroom,实现了发送图文消息.表情(gif图), ...
随机推荐
- ●HDU 5608 function
题链: http://acm.hdu.edu.cn/showproblem.php?pid=5608 题解: 莫比乌斯反演,杜教筛 已知$$N^2-3N+2=\sum_{d|N} f(d)$$ 多次询 ...
- 【Ural1277】 Cops and Thieves 无向图点连通度问题
1277. Cops and Thieves Time limit: 1.0 secondMemory limit: 64 MB The Galaxy Police (Galaxpol) found ...
- bzoj 2212: [Poi2011]Tree Rotations
Description Byteasar the gardener is growing a rare tree called Rotatus Informatikus. It has some in ...
- hdu 5464(dp)
题意: 给你n个数,要求选一些数(可以不选),把它们加起来,使得和恰好是p的倍数(0也是p的倍数),求方案数. - - 心好痛,又没想到动规 #include <stdio.h> #inc ...
- SpringMVC 处理映射
一.Spring MVC控制器名称处理映射 以下示例展示如何利用Spring MVC 框架使用控制器名称处理程序映射. ControllerClassNameHandlerMapping类是基于约定的 ...
- Spring MVC页面重定向
以下示例显示如何编写一个简单的基于Web的重定向应用程序,这个应用程序使用重定向将http请求传输到另一个页面. 基于Spring MVC - Hello World实例章节中代码,创建创建一个名称为 ...
- Selenium之unittest测试框架详谈及实操
申明:本文是基于python3.x及selenium3.x. unittest,也可以称为PyUnit,可以用来创建全面的测试套件,可以用于单元自动化测试(模块).功能自动化测试(UI)等等. 官方文 ...
- Luogu P2756 [网络流24题]飞行员配对方案问题_二分图匹配
二分图模板题 我用的是匈牙利 其实最大流也可以做 #include<iostream> #include<cstdio> #include<cstdlib> #in ...
- IF判断条件说明
在Python中,任何非零整数都为true,0是false:判断条件也可以是任何序列(列表.元组.字符串):所有长度不为零的为true,否则为false,比如:空序列为false.简而言之:非0非空为 ...
- Docker 容器格式
最初,Docker 采用了 LXC 中的容器格式.自 1.20 版本开始,Docker 也开始支持新的 libcontainer 格式,并作为默认选项. 对更多容器格式的支持,还在进一步的发展中.