栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android控件PopupWindow模仿ios底部弹窗

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android控件PopupWindow模仿ios底部弹窗

前言

在H5火热的时代,许多框架都出了底部弹窗的控件,在H5被称为弹出菜单ActionSheet,今天我们也来模仿一个ios的底部弹窗,取材于苹果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());
  //设置点击弹窗外隐藏自身
  popupWindow.setFocusable(true);
  popupWindow.setOutsideTouchable(true);
  //设置动画
  popupWindow.setAnimationStyle(R.style.PopupWindow);
  //设置位置
  popupWindow.showAtLocation(v, Gravity.BOTTOM, 0, navigationHeight);
  //设置消失监听
  popupWindow.setonDismissListener(this);
  //设置PopupWindow的View点击事件
  setonPopupViewClick(view);
  //设置背景色
  setBackgroundAlpha(0.5f);
}

步骤分析:

PopupWindow的布局
PopupWindow的创建
PopupWindow添加动画效果
PopupWindow设置背景阴影
PopupWindow监听点击事件
获取NavigationBar的高度

PopupWindow的布局:在Layout中,设计我们需要的布局




  

    

    

    

    

    
  

  

    
  


其效果是:


PopupWindow的创建:这个创建与我们普通的控件创建方法是一样的

//设置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添加动画效果:我们创建一个anim文件夹,创建我们的out和in动画效果,然后在style添加我们的动画










 @anim/window_in @anim/window_out 

//设置动画
popupWindow.setAnimationStyle(R.style.PopupWindow);

PopupWindow设置背景阴影:弹窗打开时设置透明度为0.5,弹窗消失时设置透明度为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);

对存在NavigationBar的手机上,设置其PopupWindow的出现位置
//设置位置
popupWindow.showAtLocation(v, Gravity.BOTTOM, 0, navigationHeight);

对没有NavigationBar的手机上,设置其PopupWindow的出现位置
//设置位置
popupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);

源码

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", "dimen", "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.MATCH_PARENT,
 RelativeLayout.LayoutParams.WRAP_CONTENT);
    //设置背景,这个没什么效果,不添加会报错
    popupWindow.setBackgroundDrawable(new BitmapDrawable());
    //设置点击弹窗外隐藏自身
    popupWindow.setFocusable(true);
    popupWindow.setOutsideTouchable(true);
    //设置动画
    popupWindow.setAnimationStyle(R.style.PopupWindow);
    //设置位置
    popupWindow.showAtLocation(v, Gravity.BOTTOM, 0, navigationHeight);
    //设置消失监听
    popupWindow.setonDismissListener(this);
    //设置PopupWindow的View点击事件
    setonPopupViewClick(view);
    //设置背景色
    setBackgroundAlpha(0.5f);
  }

  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);
  }

  //设置屏幕背景透明效果
  public void setBackgroundAlpha(float alpha) {
    WindowManager.LayoutParams lp = getWindow().getAttributes();
    lp.alpha = alpha;
    getWindow().setAttributes(lp);
  }

  @Override
  public void onDismiss() {
    setBackgroundAlpha(1);
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/160969.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号