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

Android自定义popupwindow实例代码

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

Android自定义popupwindow实例代码

先来看看效果图:

一、布局 




  

  



2、自定义MypopupWindow继承PopupWindow

public class MyPopupWindow extends PopupWindow {  

3、重写构造方法与动画样式
在styles.xml自定义样式,动画



    @anim/pop_in
    @anim/pop_out
  

 pop_in




  

  

  

  
  



pop_out




  

  



  



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


    
    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;



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


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


    
    initView();

    initListener();
  }




  private void initView() {

  }

  private void initListener() {

  }


}

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

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

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

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