android popupwindow 调用方法,Android简单使用PopupWindow的方法

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

思路

1.在res下面创建一个menu文件夹,并新建一个xml文件作为PoupWindow的布局文件。

2.Activity中布局填充器加载菜单布局

3.创建PopupWindow对象并设置内容以及动画

4.设置菜单布局中控件需要做的操作

menu菜单布局:

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#eaeaea"

android:orientation="vertical">

android:id="@+id/bt1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="10dp"

android:text="选项一" />

android:id="@+id/bt2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="10dp"

android:text="选项二" />

android:id="@+id/bt3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="10dp"

android:text="选项三" />

定义动画:

在styles资源文件里写一个style继承android:Animation,并设置进出场效果

内容引用res下面的anim资源文件夹里的资源文件

将动画绑定到popuwidow

styles资源文件

8811115bde4b0cd06a9f1d1fcb45dfa3.png

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

anim资源文件

14b54ec0169f50ead67899c7ede2af6b.png

83c4bef1ce344f1e0ec6b0b307a68de4.png

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

}

}

ffb11e0f68f3656d7ba91fe6bb32808c.png

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值