先来看看效果图:
一、布局
<?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="wrap_content" android:orientation="vertical" android:background="#ffffff" android:padding="20dp" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:clickable="true" android:gravity="center" android:textColor="@android:color/holo_orange_dark" android:text="确定" /> <TextView android:layout_marginTop="20dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:clickable="true" android:gravity="center" android:text="取消" /> </LinearLayout>
2、自定义MypopupWindow继承PopupWindow
public class MyPopupWindow extends PopupWindow {
3、重写构造方法与动画样式
在styles.xml自定义样式,动画
<style name="MyPopupWindow"> <item name="android:windowEnterAnimation">@anim/pop_in</item> <item name="android:windowExitAnimation">@anim/pop_out</item> </style>
pop_in
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 平移 <translate android:duration="5000" android:fromXDelta="100%" android:toXDelta="0"/> --> <scale android:fromXScale="0" android:fromYScale="0" android:pivotX="50%" android:pivotY="50%" android:toXScale="0.8" android:toYScale="0.5" android:duration="200"/> <!-- fromXScale fromYScale 起始时X,Y座标, pivotX pivotY 动画起始位置,相对于屏幕的百分比,两个都为50%表示动画从屏幕中间开始 toXScale toYScale 动画最终缩放的倍数, 1.0为正常大小,大于1.0放大 duration 动画持续时间 --> <!--透明度--> <alpha android:duration="200" android:fromAlpha="0.0" android:toAlpha="1.0"/> </set>
pop_out
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <!-- <translate android:duration="5000" android:fromXDelta="0" android:toXDelta="100%"/>--> <scale android:fromXScale="0.8" android:fromYScale="0.5" android:pivotX="50%" android:pivotY="50%" android:toXScale="0" android:toYScale="0" android:duration="200"/> <alpha android:duration="200" android:fromAlpha="1.0" android:toAlpha="0.0"/> </set>
4、重写构造方法并设置点击外部可以消失监听
super(context); this.mContext=context; //打气筒 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //打气 mContentView = mInflater.inflate(R.layout.layout_dialog,null); //设置View setContentView(mContentView); //设置宽与高 setWidth(WindowManager.LayoutParams.MATCH_PARENT); setHeight(WindowManager.LayoutParams.WRAP_CONTENT); /** * 设置进出动画 */ setAnimationStyle(R.style.MyPopupWindow); /** * 设置背景只有设置了这个才可以点击外边和BACK消失 */ setBackgroundDrawable(new ColorDrawable()); /** * 设置可以获取集点 */ setFocusable(true); /** * 设置点击外边可以消失 */ setOutsideTouchable(true); /** *设置可以触摸 */ setTouchable(true); /** * 设置点击外部可以消失 */ setTouchInterceptor(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { /** * 判断是不是点击了外部 */ if(event.getAction()==MotionEvent.ACTION_OUTSIDE){ return true; } //不是点击外部 return false; } });
5、显示及设置窗口变暗与变亮
public void displayDialog(View view){ MyPopupWindow myPopupWindow = new MyPopupWindow(this); myPopupWindow.showAsDropDown(mBtnDispaly,0,0); lightOff(); /** * 消失时屏幕变亮 */ myPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); layoutParams.alpha=1.0f; getWindow().setAttributes(layoutParams); } }); } /** * 显示时屏幕变暗 */ private void lightOff() { WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); layoutParams.alpha=0.3f; getWindow().setAttributes(layoutParams); }
6、完整
package liu.basedemo.view; import android.content.Context; import android.graphics.drawable.ColorDrawable; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; import android.widget.PopupWindow; import liu.basedemo.R; /** * 学习PopupWindow * Created by 刘楠 on 2016/8/1 0001.17:42 */ public class MyPopupWindow extends PopupWindow { Context mContext; private LayoutInflater mInflater; private View mContentView; public MyPopupWindow(Context context) { super(context); this.mContext=context; //打气筒 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //打气 mContentView = mInflater.inflate(R.layout.layout_dialog,null); //设置View setContentView(mContentView); //设置宽与高 setWidth(WindowManager.LayoutParams.MATCH_PARENT); setHeight(WindowManager.LayoutParams.WRAP_CONTENT); /** * 设置进出动画 */ setAnimationStyle(R.style.MyPopupWindow); /** * 设置背景只有设置了这个才可以点击外边和BACK消失 */ setBackgroundDrawable(new ColorDrawable()); /** * 设置可以获取集点 */ setFocusable(true); /** * 设置点击外边可以消失 */ setOutsideTouchable(true); /** *设置可以触摸 */ setTouchable(true); /** * 设置点击外部可以消失 */ setTouchInterceptor(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { /** * 判断是不是点击了外部 */ if(event.getAction()==MotionEvent.ACTION_OUTSIDE){ return true; } //不是点击外部 return false; } }); /** * 初始化View与监听器 */ initView(); initListener(); } private void initView() { } private void initListener() { } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。
本文向大家介绍PopupWindow自定义位置显示的实现代码,包括了PopupWindow自定义位置显示的实现代码的使用技巧和注意事项,需要的朋友参考一下 一、概述 在Android中弹出式菜单(以下称弹窗)是使用十分广泛的一种菜单呈现方式,弹窗为用户交互提供了便利。关于弹窗的实现大致有以下两种方式AlertDialog和PopupWindow,当然网上也有使用Activity并配合Dialog主
本文向大家介绍Android自定义ActionBar实例,包括了Android自定义ActionBar实例的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Android自定义ActionBar的实现方法。分享给大家供大家参考。具体实现方法如下: Android 3.0及以上已经有了ActionBar的API,可以通过引入support package在3.0以下的平台引用这些API,但这儿
本文向大家介绍Android自定义控件案例汇总1(菜单、popupwindow、viewpager),包括了Android自定义控件案例汇总1(菜单、popupwindow、viewpager)的使用技巧和注意事项,需要的朋友参考一下 自定义控件是根据自己的需要自己来编写控件。安卓自带的控件有时候无法满足你的需求,这种时候,我们只能去自己去实现适合项目的控件。同时,安卓也允许你去继承已经存在的控件
本文向大家介绍Android 自定义View之倒计时实例代码,包括了Android 自定义View之倒计时实例代码的使用技巧和注意事项,需要的朋友参考一下 Android 自定义View之倒计时实例代码 需求: 大多数app在注册的时候,都有一个获取验证码的按钮,点击后,访问接口,最终用户会收到短信验证码。为了不多次写这个获取验证码的接口,下面将它自定义成一个view,方便使用。 分析一下,这是一
本文向大家介绍Android自定义view实现太极效果实例代码,包括了Android自定义view实现太极效果实例代码的使用技巧和注意事项,需要的朋友参考一下 Android自定义view实现太极效果实例代码 之前一直想要个加载的loading。却不知道用什么好,然后就想到了太极图标,最后效果是有了,不过感觉用来做loading简直丑到爆!!! 实现效果很简单,我们不要用什么贝塞尔曲线啥的,因为太
本文向大家介绍Android PopupWindow全屏详细介绍及实例代码,包括了Android PopupWindow全屏详细介绍及实例代码的使用技巧和注意事项,需要的朋友参考一下 Android PopupWindow全屏 很多应用中经常可以看到弹出这种PopupWindow的效果,做了一个小demo分享一下。demo的思路是通过遍历文件,找到图片以及图片文件夹放置在PopupWindow上
本文向大家介绍Android自定义DataTimePicker实例代码(日期选择器),包括了Android自定义DataTimePicker实例代码(日期选择器)的使用技巧和注意事项,需要的朋友参考一下 笔者有一段时间没有发表关于Android的文章了,关于Android自定义组件笔者有好几篇想跟大家分享的,后期会记录在博客中。本篇博客给大家分享的是自定义一个日期选择器,可以让用户同时选择年月日和
本文向大家介绍Android自定义dialog 自下往上弹出的实例代码,包括了Android自定义dialog 自下往上弹出的实例代码的使用技巧和注意事项,需要的朋友参考一下 具体代码如下所示: 总结 以上所述是小编给大家介绍的Android自定义dialog 自下往上弹出的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对呐喊教程网站的支持!