PopupWindow弹窗demo

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="pop" />

</LinearLayout>


 

自定义View

pop_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button_popupe"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dip"
        android:text="pop,pop" >
    </Button>

</LinearLayout>


 

anim文件下自定义动画

fipper_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="1000"
        android:fromYDelta="100%p"
        android:toYDelta="0" />

    <alpha
        android:duration="1000"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />

</set>


 

fipper_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="1000"
        android:fromYDelta="0"
        android:toYDelta="100%p" />

    <alpha
        android:duration="1000"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />

</set>


ActivityMain

package com.android;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.ViewFlipper;

public class ActivityMain extends Activity {
	//定义PopupWindow
	private PopupWindow popup;
	//定义PopupWindow动画
	private ViewFlipper mViewFlipper;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // 创建动画
		mViewFlipper = new ViewFlipper(this);
		mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
				R.anim.flipper_in));
		mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
				R.anim.flipper_out));
		//获取自定义的pop_view,因为pop_view.xml没有设置setContentView方法,所以不能用findViewById方法获取对象
		LayoutInflater mLayoutInflater = LayoutInflater.from(getApplicationContext());
		View pop_view = mLayoutInflater.inflate(R.layout.pop_view, null);
		mViewFlipper.addView(pop_view);
		mViewFlipper.setFlipInterval(3000);
		// 创建Popup
		popup = new PopupWindow(mViewFlipper, LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT);
		popup.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.pop_bg));
		popup.setFocusable(true);
		popup.update();
		//开启pop的按钮
		Button btn = (Button)this.findViewById(R.id.button1);
		btn.setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View v) {
					if (popup != null) {
						if (popup.isShowing()) {
							popup.dismiss();
						} else {
							//相对与某个控件的位置
						    popup.showAtLocation(v,Gravity.BOTTOM, 0, 0);
						    //播放动画
						    mViewFlipper.startFlipping();
						}
					}
				}
				
			});
		
    }
}


点击pop按钮即可出现下面pop.pop弹窗。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值