來自:http://android.tgbus.com/Android/tutorial/201106/357509.shtml
彈出popwindow的代碼如下,比較亂,多包涵:
popupWindow = new PopupWindow(menuView, LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT, true);
popupWindow.showAtLocation(findViewById(R.id.parent), Gravity.CENTER
| Gravity.CENTER, 0, 0);
popupWindow.setAnimationStyle(R.style.PopupAnimation);
// 加上下面兩行可以用back鍵關閉popupwindow,否則必須調用dismiss();
ColorDrawable dw = new ColorDrawable(-00000);
popupWindow.setBackgroundDrawable(dw);
popupWindow.update();
下面是實現步驟:
1.背景置灰:
popupWindow = new PopupWindow(menuView, LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT, true);
第二三個參數必須是LayoutParams.FILL_PARENT,這樣才能填充整個螢幕,達到背景置灰的目的。
整個popupwindow裡面是一個GridView,圖片什麼的也是用的那個仿UC瀏覽器介面項目的,在此謝謝了。
關鍵的東西都在xml裡面。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:gravity="center" android:layout_height="fill_parent"
android:layout_gravity="center" android:background="#b0000000" >
<LinearLayout android:orientation="vertical"
android:layout_width="wrap_content" android:gravity="center"
android:layout_height="wrap_content" android:layout_gravity="center"
android:background="@drawable/downbutton_corner">
<GridView android:id="@+id/gridview" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:numColumns="4"
android:verticalSpacing="5dip" android:horizontalSpacing="5dip"
android:stretchMode="columnWidth" android:gravity="center"
android:layout_gravity="center" />
</LinearLayout>
</LinearLayout>
第一個linearlayout裡面的android:background="#b0000000",就是全屏背景,網上搜的好多半透明都是“#e0000000”,我覺得那顏色太深,“#b0000000”更合適。
第二個linearlayout是popupwind的背景,裡面的android:background="@drawable/downbutton_corner"是關鍵,邊框,圓角都是裡面定義的。
2.popupwindow的邊框,圓角背景。downbutton_corne.xmlpopupwindow的邊框,圓角背景。
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#c0000000" android:endColor="#c0000000"
android:angle="90" /><!--背景色彩坡形 -->
<stroke android:dashWidth="2dp" android:dashGap="2dp"
android:width="2dp" android:color="#FF00ff00"></stroke>
<!--描邊 -->
<corners android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp" android:topLeftRadius="5dp"
android:topRightRadius="5dp" /><!--設定圓角-->
</shape>
<gradient android:startColor="#c0000000" android:endColor="#c0000000" android:angle="90" /><!--背景色彩坡形 -->
我就設定了一個固定的顏色"#c0000000"。android:angle="90"這個是設定色彩坡形方向,從上到下啊,從左至右啊,貌似只能90的倍數,也只有四個方向嘛。
<stroke ></stroke>,邊框就是這個實現的。
dashWidth指的是邊線的寬度 dashGap 指的是每條線之間的間距,(因為是邊線是很多小橫線組成的)。
3.淡入淡齣動畫
popupWindow.setAnimationStyle(R.style.PopupAnimation);
這條代碼是設定style的,動畫檔案就是在style檔案裡面引入的。下面是淡入的動畫,動畫教程網上也很多。淡出的動畫就這些參數值交換位置就是了。android:duration這個是期間,為了,我把它弄成5秒了。
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale android:fromXScale="0.6" android:toXScale="1.0"
android:fromYScale="0.6" android:toYScale="1.0" android:pivotX="50%"
android:pivotY="50%" android:duration="5000" />
<alpha android:interpolator="@android:anim/decelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="5000" />
</set>
還有一個關鍵的問題。彈出pop之後,back鍵無效了,必須在pop裡面設定事件dismiss掉。