Android简单使用PopupWindow的方法

作者:天真的赵日天 时间:2023-09-13 19:51:22 

本文实例为大家分享了Android简单使用PopupWindow的的具体代码,供大家参考,具体内容如下

思路

1.在res下面创建一个menu文件夹,并新建一个xml文件作为PoupWindow的布局文件。
2.Activity中布局填充器加载菜单布局
3.创建PopupWindow对象并设置内容以及动画
4.设置菜单布局中控件需要做的操作

menu菜单布局:


<?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="match_parent"
android:background="#eaeaea"
android:orientation="vertical">

<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="选项一" />

<Button
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="选项二" />

<Button
android:id="@+id/bt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="选项三" />
</LinearLayout>

定义动画:

在styles资源文件里写一个style继承android:Animation,并设置进出场效果
内容引用res下面的anim资源文件夹里的资源文件
将动画绑定到popuwidow

styles资源文件

Android简单使用PopupWindow的方法

在res下面创建一个anim资源文件夹

anim资源文件

Android简单使用PopupWindow的方法

Android简单使用PopupWindow的方法

popupwindow绑定动画 popupWindow.setAnimationStyle(R.style.www);

Activity


public class MainActivity extends AppCompatActivity {
private Button bt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = findViewById(R.id.bt);
}

public void click(View view) { //点击事件
View inflate = LayoutInflater.from(this).inflate(R.layout.menu, null); //布局加载器加载菜单布局
Button bt1 = inflate.findViewById(R.id.bt1);
Button bt2 = inflate.findViewById(R.id.bt2);
Button bt3 = inflate.findViewById(R.id.bt3);
/**
* 创建PopupWindow对象 视图对象, 宽, 高,缺一不可
* 第一种:创建对象的时候直接加参数 PopupWindow popupWindow = new PopupWindow(inflate, 200, ViewGroup.LayoutParams.WRAP_CONTENT,true);
* 第二种 通过setContentView,setHeight,setWidth 来设置
* 宽高可设置固定值或者ViewGroup.LayoutParams.WRAP_CONTENT
**/
final PopupWindow popupWindow = new PopupWindow(inflate, 200, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setOutsideTouchable(true); //点击弹窗外部是否取消弹窗
popupWindow.setAnimationStyle(R.style.www); //设置自定义好的动画
//弹窗出现外部为阴影
WindowManager.LayoutParams attributes = getWindow().getAttributes();
attributes.alpha = 0.5f;
getWindow().setAttributes(attributes);
//弹窗取消监听 取消之后恢复阴影
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams attributes = getWindow().getAttributes();
attributes.alpha = 1;
getWindow().setAttributes(attributes);
}
});
//选项的点击事件
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
popupWindow.dismiss();
Toast.makeText(MainActivity.this, "选项一", Toast.LENGTH_SHORT).show();

}
});
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
popupWindow.dismiss();
Toast.makeText(MainActivity.this, "选项二", Toast.LENGTH_SHORT).show();

}
});
bt3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
popupWindow.dismiss();
Toast.makeText(MainActivity.this, "选项三", Toast.LENGTH_SHORT).show();

}
});
/**
* 显示popupwidow两种方式
* 1.howAsDropDown出现在下方,三个参数:1绑定的控件2.x轴偏移量 3.y轴偏移量
* 2.showAtLocation自定义位置 四个参数 1绑定的控件 2.出现的位置 3.x轴偏移量 4.y轴偏移量
*
**/
// popupWindow.showAsDropDown(bt,0,0);
popupWindow.showAtLocation(bt, Gravity.CENTER, 0, 0);
}
}

Android简单使用PopupWindow的方法

来源:https://blog.csdn.net/wjp52/article/details/108515313

标签:Android,PopupWindow
0
投稿

猜你喜欢

  • android 获取上一个activity返回值的方法

    2023-06-15 13:22:22
  • Java基础之位运算知识总结

    2022-09-10 17:48:43
  • Spring事务管理配置文件问题排查

    2022-07-04 15:55:25
  • 详解Spring Security如何配置JSON登录

    2023-02-08 17:39:07
  • Java数据结构与算法之单链表深入理解

    2023-12-02 05:53:12
  • C语言实现顺序表的顺序查找和折半查找

    2021-10-24 13:17:19
  • ImageSwitcher图像切换器的使用实例

    2022-10-29 13:16:02
  • c#中使用BackgroundWorker的实现

    2023-05-04 08:51:01
  • MyBatis 多个条件使用Map传递参数进行批量删除方式

    2023-11-29 08:09:01
  • Java编程将汉字转Unicode码代码示例

    2023-11-09 17:33:39
  • Java初学者问题图解(动力节点Java学院整理)

    2023-10-15 18:06:11
  • java编程实现根据EXCEL列名求其索引的方法

    2022-04-24 03:08:03
  • 详解java注解相关知识

    2022-09-22 12:32:52
  • Android Studio实现进度条效果

    2021-10-12 05:37:17
  • WPF TextBox和PasswordBox添加水印

    2021-09-30 16:06:02
  • C#设置页面单位和缩放的方法

    2023-06-09 12:59:27
  • 基于c# 接口的实例详解

    2023-10-22 22:22:11
  • C#中ArrayList的使用方法

    2023-01-26 10:20:47
  • 基于java实现简单发红包功能

    2023-08-09 17:06:40
  • C#中List集合使用Max()方法查找到最大值的实例

    2023-12-01 08:03:32
  • asp之家 软件编程 m.aspxhome.com