Android PopupWindow的使用

本文详细介绍了在Android应用中如何使用PopupWindow组件展示一个包含ListView的弹窗,包括布局设置、数据填充、动画效果配置及显示与隐藏机制。

在Android中使用PopupWindow并不复杂,先看效果图,我们要点击按钮后让PopupWindow弹出来,PopupWindow中是一个ListView,效果图如下:


下面是代码:

package com.example.testpopupwindow;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.drawable.PaintDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;

public class MainActivity extends Activity {
	private PopupWindow popWin;
	private List<String> strs = new ArrayList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //为ListView添加数据
        for(int i =0; i < 10; i++){
        	strs.add("item " + i);
        }
        
        popWin = new PopupWindow(this);
        //获取popWin要显示的布局
	View contentView = LayoutInflater.from(this).inflate(R.layout.pop_win_layout, null);
	ListView listView = (ListView) contentView.findViewById(R.id.pop_win_listview);
	listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strs));
	//为popWin设置布局
	popWin.setContentView(contentView);
	//不指定popWin的大小就无法显示popWin
	popWin.setWidth(300);
	popWin.setHeight(500);
	//加上下面三句就可以在popWin外点击让popWin消失,同时popWin内也可以获取焦点
	popWin.setFocusable(true);
	popWin.setOutsideTouchable(true);
	popWin.setBackgroundDrawable(new PaintDrawable());
	//指定popWin显示和消失的动画
	popWin.setAnimationStyle(R.style.PopWinAnimation);
    }
    
    //点击按钮显示PopupWindow
    public void btnOnClick(View view){
    	//显示popWin,在按钮的正下方
    	popWin.showAtLocation(view, Gravity.CENTER, 0, 10);
    }

}
在上面的代码中,onCreate方法里创建了PopupWindow,并指定了它的布局和大小,最后还为PopupWindow指定了显示和消失的动画,这里使用的是平移动画,要使用动画,我们先要在res/anim文件夹(没有的话新建一个文件夹)下创建动画的xml文件,下面是PopupWindow的动画文件:

这是pop_show.xml  :

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="500"
        android:toXDelta="0"
        android:fromYDelta="0"
        android:toYDelta="0"
        android:duration="500"
        />
    
</set>
这个是pop_hide.xml  :

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="0"
        android:toXDelta="500"
        android:fromYDelta="0"
        android:toYDelta="0"
        android:duration="500"
        />
</set>
定义好动画文件之后,我们还需要在res/values/styles.xml文件中添加一个style,代码如下:

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="AppTheme" parent="android:Theme.Light" />

    <style name="PopWinAnimation">
        <item name="android:windowEnterAnimation">@anim/pop_show</item>
        <item name="android:windowExitAnimation">@anim/pop_hide</item>
    </style>
</resources>
其中的PopWinAnimation就是我们指定的窗口进入和退出的动画,然后在Java代码中,为PopupWindow添加动画效果的代码就是:

popWin.setAnimationStyle(R.style.PopWinAnimation);
源码下载点击这里






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yubo_725

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值