安卓弹出窗口案例

一.弹出窗体

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>

猜你喜欢

转载自blog.csdn.net/jiang_xinxing/article/details/79347949
今日推荐