安卓弹出窗口案例

一.弹出窗体

1.弹出窗体一定注意关闭
2.弹出窗体要播放动画,必须有背景资源

二.源代码

MainActivity
package com.itheima62.popupwindowdemo;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.PopupWindow;
import android.widget.RelativeLayout.LayoutParams;

public class MainActivity extends Activity {

	private PopupWindow pw;
	private AlphaAnimation aa;
	private View contentView;
	private ScaleAnimation sa;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initPopup();
	}
	
	/**
	 * 初始化弹出窗体
	 */
	private void initPopup() {
		contentView = View.inflate(getApplicationContext(), R.layout.popup, null);

        pw = new PopupWindow(contentView , -2, -2);
		
       sa = new ScaleAnimation(
    		   1, 1, 
    		   0, 1, 
    		   Animation.RELATIVE_TO_SELF, 0.5f, 
    		   Animation.RELATIVE_TO_SELF, 0f );
       sa.setDuration(1000);
        
        
		
	}

	/**
	 * 弹出窗体
	 * @param v
	 */
	public void popupWindow(View v){
		if ( pw != null && pw.isShowing()) {
			pw.dismiss();
		} else {
			int[] location = new int[2];
			/**
			 * 1,父组件
			 * 2,对齐方式
			 * 3, x坐标
			 * 4,y坐标
			 */
			v.getLocationInWindow(location );
			System.out.println("x:" + location[0] + ",y:" + location[1]);
			//设置弹出窗体的背景
			pw.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
			contentView.startAnimation(sa);
			pw.showAtLocation(v, Gravity.LEFT | Gravity.TOP, location[0] + v.getWidth(), location[1] + v.getHeight());
			
		}
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		if (pw != null && pw.isShowing()) {
			pw.dismiss();
			pw = null;
		}
		super.onDestroy();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
layout
activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:onClick="popupWindow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="弹出窗体" />

</RelativeLayout>
popup.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#ff3311"
    android:orientation="vertical" >

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckBox" />

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

    <ProgressBar
        android:id="@+id/progressBar2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.itheima62.popupwindowdemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.itheima62.popupwindowdemo.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">PopupWindowDemo</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>

</resources>

关注问我技术公众号,加小问,拉您入技术交流群:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

The_Web3_社区

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

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

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

打赏作者

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

抵扣说明:

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

余额充值