安卓彈出視窗案例
阿新 • • 發佈:2019-02-18
一.彈出窗體
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>