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 1576】 A/B
Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1). Input 数据的 ...
- ●POJ 1556 The Doors(简单计算几何+最短路)
●赘述题目 10*10的房间内,有竖着的一些墙(不超过18个).问从点(0,5)到(10,5)的最短路. 按照输入样例,输入的连续5个数,x,y1,y2,y3,y4,表示(x,0--y1),(x,y2 ...
- ●BZOJ 4289 PA2012 Tax
●赘述题目 算了,题目没有重复的必要. 注意理解:对答案造成贡献的是每个点,就是了. 举个栗子: 对于如下数据: 2 1 1 2 1 答案是 2: ●题解 方法:建图(难点)+最短路. 先来几个链接: ...
- bzoj4830 hnoi2017 抛硬币
题目描述 小 A 和小 B 是一对好朋友,他们经常一起愉快的玩耍.最近小 B 沉迷于**师手游,天天刷本,根本无心搞学习.但是已经入坑了几个月,却一次都没有抽到 SSR,让他非常怀疑人生.勤勉的小 A ...
- poj3185 高斯消元
The Water Bowls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5329 Accepted: 2081 D ...
- [APIO2011]
来自FallDream的博客,未经允许,请勿转载,谢谢. ------------------------------------------------------ A.[Apio2011]方格染色 ...
- Java多线程基础总结
一.线程和进程关系 二.创建方式1.继承Thread类,重写run方法2.实现Runable接口,重写run方法3.使用匿名内部类 三.API接口start()currentThread() 获取当前 ...
- 腾讯笔试题:小Q硬币组合
腾讯有一道机试题: 大概意思是: 小Q非常富有,拥有非常多的硬币,小Q的拥有的硬币是有规律的,对于所有的非负整数K,小Q恰好> 各有两个数值为2^k,的硬币,所以小Q拥有的硬币是1,1,2,2, ...
- ZhuSuan 是建立在Tensorflow上的贝叶斯深层学习的 python 库
ZhuSuan 是建立在Tensorflow上的贝叶斯深层学习的 python 库. 与现有的主要针对监督任务设计的深度学习库不同,ZhuSuan 的特点是深入到贝叶斯推理中,从而支持各种生成模式:传 ...
- 吴恩达深度学习第2课第3周编程作业 的坑(Tensorflow+Tutorial)
可能因为Andrew Ng用的是python3,而我是python2.7的缘故,我发现了坑.如下: 在辅助文件tf_utils.py中的random_mini_batches(X, Y, mini_b ...