popupwindow弹出窗关闭_弹出窗口:PopupWindow 详解

效果如图所示,点击开始按钮,popWindow从下往上出来,再点击popWindow外面,popWindow又从上往下消失

可以看出来,上面的popupWindow是半透明的,后面我会细说。

最基本的是activity_main了,很简单,就只是一个button,这里我就不贴代码了。

接下来的是,popWindow的界面了

代码如下: 这里注意我里面的那个注释

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_margin="5dp"

android:orientation="vertical" >

android:id="@+id/first"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginBottom="5dp"

android:layout_marginLeft="10dp"

android:layout_marginRight="10dp"

android:layout_marginTop="5dp"

android:background="@android:color/holo_red_light"

android:text="第一个按钮" />

android:id="@+id/second"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginBottom="5dp"

android:layout_marginLeft="10dp"

android:layout_marginRight="10dp"

android:background="@android:color/holo_red_light"

android:text="第二个按钮" />

android:id="@+id/third"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginBottom="5dp"

android:layout_marginLeft="10dp"

android:layout_marginRight="10dp"

android:background="@android:color/holo_red_light"

android:text="第三个按钮" />

然后在res/下新建一个文件夹anim,进而anim下新建两个xml文件,如图所示:

其中,pophidden_anim的代码如下

android:duration="1000"

android:fromYDelta="0"

android:toYDelta="50%p" />

android:duration="1000"

android:fromAlpha="1.0"

android:toAlpha="0.0" />

popshow_anim的代码如下

android:duration="1000"

android:fromYDelta="100%p"

android:toYDelta="0" />

android:duration="1000"

android:fromAlpha="0.0"

android:toAlpha="1.0" />

然后在values/styles.xml加入以下代码,变成这个样子,上面的那些是自带的

Theme customizations available in newer API levels can go in

res/values-vXX/styles.xml, while customizations related to

backward-compatibility can go here.

-->

@anim/pophidden_anim

之后就是Activity里面的代码了,我里面都写好了所有的注释,应该可以看得很清楚

package com.example.popwindow;

import android.os.Bundle;

import android.app.Activity;

import android.content.Context;

import android.graphics.drawable.BitmapDrawable;

import android.graphics.drawable.ColorDrawable;

import android.view.Gravity;

import android.view.LayoutInflater;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.WindowManager;

import android.widget.Button;

import android.widget.PopupWindow;

import android.widget.PopupWindow.OnDismissListener;

import android.widget.Toast;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button start = (Button) findViewById(R.id.start);

start.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

showPopwindow();

}

});

}

/**

* 显示popupWindow

*/

private void showPopwindow() {

// 利用layoutInflater获得View

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View view = inflater.inflate(R.layout.popwindowlayout, null);

// 下面是两种方法得到宽度和高度 getWindow().getDecorView().getWidth()

PopupWindow window = new PopupWindow(view,

WindowManager.LayoutParams.MATCH_PARENT,

WindowManager.LayoutParams.WRAP_CONTENT);

// 设置popWindow弹出窗体可点击,这句话必须添加,并且是true

window.setFocusable(true);

// 实例化一个ColorDrawable颜色为半透明

ColorDrawable dw = new ColorDrawable(0xb0000000);

window.setBackgroundDrawable(dw);

// 设置popWindow的显示和消失动画

window.setAnimationStyle(R.style.mypopwindow_anim_style);

// 在底部显示

window.showAtLocation(MainActivity.this.findViewById(R.id.start),

Gravity.BOTTOM, 0, 0);

// 这里检验popWindow里的button是否可以点击

Button first = (Button) view.findViewById(R.id.first);

first.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

System.out.println("第一个按钮被点击了");

}

});

//popWindow消失监听方法

window.setOnDismissListener(new OnDismissListener() {

@Override

public void onDismiss() {

System.out.println("popWindow消失");

}

});

}

}

其中window.setFocusable(true)和window.setBackgroundDrawable()必须填写,如果是想让popWindow半透明,就是上面的那个方法,

如果只是单纯的调用这个方法就这样写window.setBackgroundDrawable(new BitmapDrawable());

关于popupWindow显示位置的具体方法,你可以看这个博客 popupWindow在指定位置上的显示

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值