iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android自定义popupwindow实例代码
  • 1197
分享到

Android自定义popupwindow实例代码

popupwindowAndroid 2022-06-06 05:06:40 1197人浏览 独家记忆
摘要

先来看看效果图: 一、布局  <?xml version="1.0" encoding="utf-8"?> <LinearL

先来看看效果图:

一、布局 


<?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);
    
    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() {
  }
}
您可能感兴趣的文章:Android 使用PopupWindow实现弹出更多的菜单实例详解Android简单实现自定义弹框(PopupWindow)Android编程实现的自定义弹窗(PopupWindow)功能示例android PopupWindow点击外部和返回键消失的解决方法Android popupwindow简单使用方法介绍Android PopupWindow全屏详细介绍及实例代码Android组件popupwindow使用方法详解Android用PopupWindow实现自定义overflowAndroid自定义仿微信PopupWindow效果android自定义popupwindow仿微信右上角弹出菜单效果Popupwindow 的简单实用案例(显示在控件下方)


--结束END--

本文标题: Android自定义popupwindow实例代码

本文链接: https://www.lsjlt.com/news/23116.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作