概念:
首先,是来自度娘的翻译,Pop:出现,突然出现,Up:上,Window:窗口,组合起来就是突出在上方的窗口。其次,这是一种对话框,可以悬浮在当前Activity上面,并可以任意 展示任意View。我们来看两个例子:
图1:支付宝的支付界面 图2:QQ的选择头像界面
那有的童鞋就不明白了,AlterDialog不是也可以实现这种功能么?对啊,实际 图1 利用AlterDialog可以简单地做出来,因为AlterDialog也可以自定义View(链接到AlterDialog自定义View)。那么图2 呢?尴尬了!咦, AlterDialog没有坐标这个设置项!有很强基础的会说可以通过Window等类和方法来设置,但是PopUpWindow更简单呦。当然这不是重点。
PopUpWindow和AlterDialog的区别:
1.组成的字母不同。这是个不得不承认的事实。
2.上面讲的,PopUpWindow天生的自由,位置随便放。AlterDialog家里管得严,每次想要移动都要征求Window的意见。
3.阻塞。PopUpWindow是阻塞式的弹框,意思是只要弹框,后面的Activity被阻塞不执行,除非执行dismiss方法。AlterDialog这个孩子很可怜,因为他没有一点点的控制权,当AlterDialog弹出对话框的时候,后面的Activity依然在执行。
4.焦点。凡事有强必有弱,PopUpWindow无法自动获取焦点,必须通过setFocusable获得焦点,而AlterDialog弹出就获得焦点。
5.AlterDialog是九兄弟,一般常说九种常用对话框,它可以直接设置标题、内容、单选框、复选框、按钮、文字等等。PopUpWindow本质就是弹出一个画好的View,并有相应的触发事件为其处理。
-----------------------图2效果实现--------------------------
接下来我们来完成图2效果:
步骤一:我们需要创建XML文件
用于弹出的View的Layout
</pre><?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/mine_change_head_lilay_bottom" android:layout_width="match_parent" android:layout_height="45sp" android:layout_alignParentBottom="true"> <TextView android:id="@+id/mine_change_head_cancel" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff" android:gravity="center" android:text="取消" android:textColor="#009930" android:textSize="18sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@id/mine_change_head_lilay_bottom" android:background="@null" android:contentDescription="@null" android:layout_marginBottom="20sp" android:orientation="vertical" android:id="@+id/linearLayout1"> <TextView android:id="@+id/mine_change_head_takePhoto" android:layout_width="match_parent" android:layout_height="45sp" android:text="拍照上传" android:gravity="center" android:textColor="#009930" android:background="#fff" android:textSize="18sp" /> <View android:layout_width="match_parent" android:layout_height="0.7dp" android:background="@null"/> <TextView android:id="@+id/mine_change_head_takeImage" android:layout_width="match_parent" android:layout_height="45sp" android:gravity="center" android:text="手机相册" android:textColor="#009930" android:background="#fff" android:textSize="18sp"/> </LinearLayout></RelativeLayout><p></p><p style="font-weight: bold;font-size:14px;"><strong><span style="font-size:14px;"></span></strong></p><p><span style="font-size:10px;color:#660000;"><strong>用于底层Activity的Layout</strong></span></p><p><span style="font-size:10px;"><strong></strong></span></p><pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/main_btn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#99251541"
android:textColor="#fff"
android:layout_marginTop="200dp"
android:textSize="18sp"
android:text="点击更换头像" />
</LinearLayout><span style="color:#660000;">
</span>
步骤二:在代码中使用
这里本来应该先加载Layout,然后初始化Layout,然后再在需要的地方使用PopUpWindow弹出,为了让复制出的代码可以直接看清逻辑,so,写到了一块。(若出现错误,是引用出了问题,替换即可)
import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Layout;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
public class PopUpWindowTest extends Activity implements OnClickListener{
/* 主Activity中要加载的控件 */
private Button main_btn=null;
/* 要弹出View中要加载的控件 */
private TextView takePhoto_btn = null;
private TextView takeImage_btn = null;
private TextView cancel = null;
/* 要弹出的View */
private View Pop=null;
/* 声明PopUpWindow对象 */
PopupWindow puw = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.test);
//进行初始化
init();
}
/**
* 初始化数据
*/
private void init()
{
main_btn = (Button)this.findViewById(R.id.main_btn);
System.out.println(main_btn);
System.out.println(this);
main_btn.setOnClickListener(this);
Pop=LayoutInflater.from(this).inflate(R.layout.mine_change_head,null);
takePhoto_btn = (TextView)Pop.findViewById(R.id.mine_change_head_takePhoto);
takeImage_btn = (TextView)Pop.findViewById(R.id.mine_change_head_takeImage);
cancel = (TextView)Pop.findViewById(R.id.mine_change_head_cancel);
takePhoto_btn.setOnClickListener(this);
takeImage_btn.setOnClickListener(this);
cancel.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v==main_btn){
//判断是否为空
if(puw == null)
{
//创建一个新的PopUpWindow
puw = new PopupWindow(Pop,LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT,true);
//设置背景
puw.setBackgroundDrawable(new ColorDrawable(0x90000200));
}
//设置显示的位置
puw.showAtLocation(v, Gravity.LEFT, 0, 0);
//这个update貌似没什么卵用,请赐教
puw.update();
}
else if(v==takePhoto_btn){
Toast.makeText(this, "拍照上传被单击了", Toast.LENGTH_SHORT).show();;
}
else if(v==takeImage_btn){
Toast.makeText(this, "手机相册被单击了", Toast.LENGTH_SHORT).show();;
}
else if(v==cancel){
puw.dismiss();//隐藏弹窗,隐藏!隐藏!隐藏!
}
}
}
这里只是简单的使用,没有设置加载时的动画效果。