1.設置半透明主題
2.設置window的alpha值
- // WindowManager.LayoutParams lp = getWindow().getAttributes();
- // lp.alpha = 0.5f; //0.0-1.0
- // getWindow().setAttributes(lp);
復制代碼
發現這兩種都不能滿足要求,起碼的顏色就不太對。想做好點,做成類似alertDialog的樣子,帶邊框,彈出窗口帶動畫效果,之后背景置灰,那多帥。
看到那個仿uc瀏覽器的源碼,是用alertdialog做的,達到那種效果,加點動畫就行了。下圖是從那個ucweb源碼里面弄出來的。
上面的代碼就不貼了,我上傳的項目文件里面也有。
下面是彈出popupwindow的圖片,第一張是動畫中,第二張是完全彈出的:
彈出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.xml
- <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>
復制代碼
這個涉及到shape畫圖,要是不懂的話。網上很多資料,搜一下就是了。我博客里面也有,
http://blog.csdn.net/ymdcr/archive/2010/12/01/6048256.aspx
<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掉。下面是問題的描述,哪位解決了,告訴我一下,謝謝。我的郵箱:ytdcr@tom.com
問題解決了,是因為沒設置背景的原因。
popupWindow.setBackgroundDrawable(new BitmapDrawable());
//把這一行放在
showAtLocation前面就行了,以前是放在后面的,粗心了。
popupWindow.showAtLocation(findViewById(R.id.parent), Gravity.CENTER
| Gravity.CENTER, 0, 0);
網上也有很多人說,彈出pop之后,不響應鍵盤事件了,這個其實是焦點在pop里面的view去了。
以這個為例,焦點就在gridview上面去了。28樓的兄弟提示的,謝了。
在gridview加上
setOnKeyListener,就能解決。
- menuGrid.setOnKeyListener(new OnKeyListener() {
- @Override
- public boolean onKey(View v, int keyCode, KeyEvent event) {
- switch (keyCode) {
- case KeyEvent.KEYCODE_MENU:
- if (popupWindow != null && popupWindow.isShowing()) {
- popupWindow.dismiss();
- System.out.println("menuGridfdsfdsfdfd");
- }
- break;
- }
-
- return true;
- }
- });
-
復制代碼
[ code]/**
* ColorDrawable dw = new ColorDrawable(-00000);
* popupWindow.setBackgroundDrawable(dw);
* 本來看了個示例,加上上面這兩行就不用調用dismiss,點擊窗口之外的部位,或者按back鍵都能關閉窗口。 但是我這樣寫了,還是不行。
* 而且竟然捕獲不到鍵盤事件,杯具,希望哪個解決了這個問題告訴我,謝謝。
ytdcr@tom.com
*/
/*
* @Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch
* (keyCode) { case KeyEvent.KEYCODE_BACK: if (popupWindow != null) {
* popupWindow.dismiss(); }
*
* Toast.makeText(this, "fd", 1000).show(); break;
*
* } return super.onKeyUp(keyCode, event); }
*/[/code]