Android实现底部弹窗效果

本文实例为大家分享了Android实现底部弹窗效果的具体代码,供大家参考,具体内容如下

创新互联建站是专业的镇原网站建设公司,镇原接单;提供网站建设、做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行镇原网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

Android实现底部弹窗效果

源代码地址:https://github.com/luoye123/Box

东西很简单,我就直接亮代码了:

1、activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


 

2、MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

 private SelectPicPopupWindow menuWindow;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 findViewById(R.id.bt_select_image).setOnClickListener(this);
 }

 @Override
 public void onClick(View view) {
 switch (view.getId()) {
  case R.id.bt_select_image:
  //TODO implement
  selectImgs();
 }
 }
 private void selectImgs(){

 menuWindow = new SelectPicPopupWindow(MainActivity.this, itemsOnClick);
 //设置弹窗位置
 menuWindow.showAtLocation(MainActivity.this.findViewById(R.id.ll_image), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
 }

 private View.OnClickListener itemsOnClick = new View.OnClickListener() {
 public void onClick(View v) {
  menuWindow.dismiss();
  switch (v.getId()) {
  case R.id.item_popupwindows_camera: //点击拍照按钮

   break;
  case R.id.item_popupwindows_Photo: //点击从相册中选择按钮


   break;
  default:
   break;
  }
 }

 };
}

3、关键代码:SelectPicPopupWindow.java

 **public class SelectPicPopupWindow extends PopupWindow {

  private Button item_popupwindows_camera, //弹窗拍照按钮
   item_popupwindows_Photo,  //弹窗从相册选择按钮
   item_popupwindows_cancel;  //弹窗取消按钮
  private View menuview;

  /**
  * 上传图片*************************
  * @param context
  * @param itemsOnclick
  */
  public SelectPicPopupWindow(Activity context, View.OnClickListener itemsOnclick){
  super(context);
  LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  menuview = inflater.inflate(R.layout.item_popupwindows,null);
  item_popupwindows_camera = (Button) menuview.findViewById(R.id.item_popupwindows_camera); //拍照按钮
  item_popupwindows_cancel = (Button) menuview.findViewById(R.id.item_popupwindows_cancel); //取消按钮
  item_**popupwindows_Photo = (Button) menuview.findViewById(R.id.item_popupwindows_Photo); //图库按钮

  /**
   * 取消按钮销毁事件
   */
  item_popupwindows_cancel.setOnClickListener(new View.OnClickListener() {
   public void onClick(View view) {
   dismiss();
   }
  });
  item_popupwindows_camera.setOnClickListener(itemsOnclick);
  item_popupwindows_Photo.setOnClickListener(itemsOnclick);
  //设置SelectPicPopupWindow的View
  this.setContentView(menuview);
  //设置SelectPicPopupWindow**弹出窗体的宽
  this.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
  //设置SelectPicPopupWindow弹出窗体的高
  //修改高度显示,解决被手机底部虚拟键挡住的问题 by黄海杰 at:2015-4-30
  this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
  //设置SelectPicPopupWindow弹出窗体可点击
  this.setFocusable(true);
  //设置SelectPicPopupWindow弹出窗体动画效果
  //this.setAnimationStyle(R.style);
  //实例化一个ColorDrawable颜色为半透明
  ColorDrawable dw = new ColorDrawable(0xb0000000);
  //设置SelectPicPopupWindow弹出窗体的背景
  this.setBackgroundDrawable(dw);
  //menuview添加ontouchlistener监听判断获取触屏位置如果在选择框外面则销毁弹出框
  menuview.setOnTouchListener(new View.OnTouchListener() {
   public boolean onTouch(View view, MotionEvent motionEvent) {
   int height = menuview.findViewById(R.id.ll_popup).getTop();
   int y = (int) motionEvent.getY();
   if (motionEvent.getAction() == MotionEvent.ACTION_UP){
    if (y

写的不好,请见谅,,下一期完成后期的工作!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


当前名称:Android实现底部弹窗效果
文章来源:http://myzitong.com/article/gdggdc.html