---
title: "Android开发窗口实现半透明效果"
author: "butter"
date: "2012-01-17T18:47:40Z"
tags: []
categories: ["移动开发"]
slug: "android"
draft: false
lastmod: "2012-01-17T18:47:40Z"
id: 1354729
url: "https://butter.iteye.com/blog/1354729"
---

[![](http://android.tgbus.com/Android/UploadFiles_4504/201106/2011062711495568.jpg)](http://www.tgbus.com/image.html?url=http%3A%2F%2Fandroid.tgbus.com%2FAndroid%2FUploadFiles_4504%2F201106%2F2011062711495568.jpg)

　　弹出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里面。

　　&lt;?xml version="1.0" encoding="utf-8"?&gt;  
　　&lt;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" &gt;  
　　         &lt;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"&gt;  
　　                 &lt;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" /&gt;  
　　　　&lt;/LinearLayout&gt;  
　　&lt;/LinearLayout&gt;

　　第一个linearlayout里面的android:background="#b0000000"，就是全屏背景，网上搜的好多半透明都是“#e0000000”，我觉得那颜色太深，“#b0000000”更合适。  
　　第二个linearlayout是popupwind的背景，里面的android:background="@drawable/downbutton\_corner"是关键，边框，圆角都是里面定义的。

　　2.popupwindow的边框，圆角背景。downbutton\_corne.xmlpopupwindow的边框，圆角背景。

　　&lt;shape xmlns:android="http://schemas.android.com/apk/res/android"  
　　        android:shape="rectangle"&gt;  
　　        &lt;gradient android:startColor="#c0000000" android:endColor="#c0000000"  
　　                android:angle="90" /&gt;&lt;!--背景颜色渐变 --&gt;  
　　        &lt;stroke android:dashWidth="2dp" android:dashGap="2dp"  
　　                android:width="2dp" android:color="#FF00ff00"&gt;&lt;/stroke&gt;  
　　        &lt;!--描边 --&gt;  
　　        &lt;corners android:bottomRightRadius="5dp"  
　　                android:bottomLeftRadius="5dp" android:topLeftRadius="5dp"  
　　                android:topRightRadius="5dp" /&gt;&lt;!--设置圆角--&gt;  
　　&lt;/shape&gt;

　　&lt;gradient android:startColor="#c0000000" android:endColor="#c0000000" android:angle="90" /&gt;&lt;!--背景颜色渐变 --&gt;  
　　我就设置了一个固定的颜色"#c0000000"。android:angle="90"这个是设置颜色渐变方向，从上到下啊，从左到右啊，貌似只能90的倍数，也只有四个方向嘛。  
　　&lt;stroke &gt;&lt;/stroke&gt;,边框就是这个实现的。  
　　dashWidth指的是边线的宽度 dashGap 指的是每条线之间的间距，（因为是边线是很多小横线组成的）。

　　3.淡入淡出动画  
　　popupWindow.setAnimationStyle(R.style.PopupAnimation);  
　　这条代码是设置style的，动画文件就是在style文件里面引入的。下面是淡入的动画，动画教程网上也很多。淡出的动画就这些参数值交换位置就是了。android:duration这个是持续时间，为了截图，我把它弄成5秒了。

　　&lt;set xmlns:android="http://schemas.android.com/apk/res/android"&gt;  
　　        &lt;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" /&gt;  
　　        &lt;alpha android:interpolator="@android:anim/decelerate\_interpolator"  
　　                android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="5000" /&gt;  
　　&lt;/set&gt;

　　还有一个关键的问题。弹出pop之后，back键无效了，必须在pop里面设置事件dismiss掉。

&lt;!--EndFragment--&gt;