前言
在H5火热的时代,许多框架都出了底部d窗的控件,在H5被称为d出菜单ActionSheet,今天我们也来模仿一个ios的底部d窗,取材于苹果QQ的选择头像功能。
正文
废话不多说,先来个今天要实现的效果图
整个PopupWindow的开启代码
private voID openPopupWindow(VIEw v) { //防止重复按按钮 if (popupWindow != null && popupWindow.isShowing()) { return; } //设置PopupWindow的VIEw VIEw vIEw = LayoutInflater.from(this).inflate(R.layout.vIEw_popupwindow,null); popupWindow = new PopupWindow(vIEw,relativeLayout.LayoutParams.MATCH_PARENT,relativeLayout.LayoutParams.WRAP_CONTENT); //设置背景,这个没什么效果,不添加会报错 popupWindow.setBackgroundDrawable(new BitmapDrawable()); //设置点击d窗外隐藏自身 popupWindow.setFocusable(true); popupWindow.setoutsIDetouchable(true); //设置动画 popupWindow.setAnimationStyle(R.style.PopupWindow); //设置位置 popupWindow.showAtLocation(v,Gravity.BottOM,navigationHeight); //设置消失监听 popupWindow.setondismissListener(this); //设置PopupWindow的VIEw点击事件 setonPopupVIEwClick(vIEw); //设置背景色 setBackgroundAlpha(0.5f);}
步骤分析:
PopupWindow的布局
PopupWindow的创建
PopupWindow添加动画效果
PopupWindow设置背景阴影
PopupWindow监听点击事件
获取Navigationbar的高度
PopupWindow的布局:在Layout中,设计我们需要的布局
<?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"> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_margin="8dp" androID:background="@drawable/popup_shape" androID:orIEntation="vertical"> <TextVIEw androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:gravity="center" androID:padding="16dp" androID:text="你可以将照片上传至照片墙" androID:textcolor="#666" androID:textSize="14sp" /> <VIEw androID:layout_wIDth="match_parent" androID:layout_height="0.1px" androID:background="#888" /> <TextVIEw androID:ID="@+ID/tv_pick_phone" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:gravity="center" androID:padding="16dp" androID:text="从手机相册选择" androID:textcolor="#118" androID:textSize="18sp" /> <VIEw androID:layout_wIDth="match_parent" androID:layout_height="0.1px" androID:background="#888" /> <TextVIEw androID:ID="@+ID/tv_pick_zone" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:gravity="center" androID:padding="16dp" androID:text="从空间相册选择" androID:textcolor="#118" androID:textSize="18sp" /> </linearLayout> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_margin="8dp" androID:background="@drawable/popup_shape"> <TextVIEw androID:ID="@+ID/tv_cancel" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:gravity="center" androID:padding="16dp" androID:text="取消" androID:textcolor="#118" androID:textSize="18sp" androID:textStyle="bold" /> </linearLayout></linearLayout>
其效果是:
PopupWindow的创建:这个创建与我们普通的控件创建方法是一样的
//设置PopupWindow的VIEwVIEw vIEw = LayoutInflater.from(this).inflate(R.layout.vIEw_popupwindow,null);popupWindow = new PopupWindow(vIEw,relativeLayout.LayoutParams.WRAP_CONTENT);
PopupWindow添加动画效果:我们创建一个anim文件夹,创建我们的out和in动画效果,然后在style添加我们的动画
<?xml version="1.0" enCoding="utf-8"?><!--进入动画--><translate xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:interpolator="@androID:anim/accelerate_interpolator" androID:fromYDelta="100%" androID:toYDelta="0" androID:duration="200"/><?xml version="1.0" enCoding="utf-8"?><!--退出动画--><translate xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:interpolator="@androID:anim/accelerate_interpolator" androID:fromYDelta="0" androID:toYDelta="100%" androID:duration="200"/><!--d窗动画--><style name="PopupWindow"> <item name="androID:windowEnteranimation">@anim/window_in</item> <item name="androID:windowExitAnimation">@anim/window_out</item> </style>
//设置动画
popupWindow.setAnimationStyle(R.style.PopupWindow);@H_403_69@
PopupWindow设置背景阴影:d窗打开时设置透明度为0.5,d窗消失时设置透明度为1
//设置屏幕背景透明效果public voID setBackgroundAlpha(float Alpha) { WindowManager.LayoutParams lp = getwindow().getAttributes(); lp.Alpha = Alpha; getwindow().setAttributes(lp);}
PopupWindow监听点击事件:监听我们PopupWindow里面控件的点击事件
//设置PopupWindow的VIEw点击事件setonPopupVIEwClick(vIEw);private voID setonPopupVIEwClick(VIEw vIEw) { TextVIEw tv_pick_phone,tv_pick_zone,tv_cancel; tv_pick_phone = (TextVIEw) vIEw.findVIEwByID(R.ID.tv_pick_phone); tv_pick_zone = (TextVIEw) vIEw.findVIEwByID(R.ID.tv_pick_zone); tv_cancel = (TextVIEw) vIEw.findVIEwByID(R.ID.tv_cancel); tv_pick_phone.setonClickListener(this); tv_pick_zone.setonClickListener(this); tv_cancel.setonClickListener(this);}
获取Navigationbar的高度:这里需要适配有些手机没有Navigationbar有些手机有,这里以存在Navigationbar来演示
int resourceID = getResources().getIDentifIEr("navigation_bar_height","dimen","androID");
navigationHeight = getResources().getDimensionPixelSize(resourceID);@H_403_69@
对存在Navigationbar的手机上,设置其PopupWindow的出现位置
//设置位置
popupWindow.showAtLocation(v,navigationHeight);@H_403_69@
对没有Navigationbar的手机上,设置其PopupWindow的出现位置
//设置位置
popupWindow.showAtLocation(v,0);@H_403_69@
源码
Github:https://github.com/AndroidHensen/IOSPopupWindow
public class MainActivity extends AppCompatActivity implements VIEw.OnClickListener,PopupWindow.OndismissListener { private button bt_open; private PopupWindow popupWindow; private int navigationHeight; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); bt_open = (button) findVIEwByID(R.ID.bt_open); bt_open.setonClickListener(this); int resourceID = getResources().getIDentifIEr("navigation_bar_height","androID"); navigationHeight = getResources().getDimensionPixelSize(resourceID); } @OverrIDe public voID onClick(VIEw v) { switch (v.getID()) { case R.ID.bt_open: openPopupWindow(v); break; case R.ID.tv_pick_phone: Toast.makeText(this,"从手机相册选择",Toast.LENGTH_SHORT).show(); popupWindow.dismiss(); break; case R.ID.tv_pick_zone: Toast.makeText(this,"从空间相册选择",Toast.LENGTH_SHORT).show(); popupWindow.dismiss(); break; case R.ID.tv_cancel: popupWindow.dismiss(); break; } } private voID openPopupWindow(VIEw v) { //防止重复按按钮 if (popupWindow != null && popupWindow.isShowing()) { return; } //设置PopupWindow的VIEw VIEw vIEw = LayoutInflater.from(this).inflate(R.layout.vIEw_popupwindow,null); popupWindow = new PopupWindow(vIEw,relativeLayout.LayoutParams.WRAP_CONTENT); //设置背景,这个没什么效果,不添加会报错 popupWindow.setBackgroundDrawable(new BitmapDrawable()); //设置点击d窗外隐藏自身 popupWindow.setFocusable(true); popupWindow.setoutsIDetouchable(true); //设置动画 popupWindow.setAnimationStyle(R.style.PopupWindow); //设置位置 popupWindow.showAtLocation(v,navigationHeight); //设置消失监听 popupWindow.setondismissListener(this); //设置PopupWindow的VIEw点击事件 setonPopupVIEwClick(vIEw); //设置背景色 setBackgroundAlpha(0.5f); } private voID setonPopupVIEwClick(VIEw vIEw) { TextVIEw tv_pick_phone,tv_cancel; tv_pick_phone = (TextVIEw) vIEw.findVIEwByID(R.ID.tv_pick_phone); tv_pick_zone = (TextVIEw) vIEw.findVIEwByID(R.ID.tv_pick_zone); tv_cancel = (TextVIEw) vIEw.findVIEwByID(R.ID.tv_cancel); tv_pick_phone.setonClickListener(this); tv_pick_zone.setonClickListener(this); tv_cancel.setonClickListener(this); } //设置屏幕背景透明效果 public voID setBackgroundAlpha(float Alpha) { WindowManager.LayoutParams lp = getwindow().getAttributes(); lp.Alpha = Alpha; getwindow().setAttributes(lp); } @OverrIDe public voID ondismiss() { setBackgroundAlpha(1); }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android控件PopupWindow模仿ios底部d窗全部内容,希望文章能够帮你解决Android控件PopupWindow模仿ios底部d窗所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)