命运掌握在自己手中。要么你驾驭生命,要么生命驾驭你,你的心态决定你是坐骑还是骑手。
本讲内容:PopupWindow 弹出窗口控件
一、PopupWindow 弹出窗口控件认识
1、Android的对话框有两种:PopupWindow和AlertDialog。
2、它们的不同点在于:
AlertDialog的位置固定,而PopupWindow的位置可以随意
AlertDialog是非阻塞线程的,而PopupWindow是阻塞线程的
3、PopupWindow的位置按照有无偏移分,可以分为偏移和无偏移两种;按照参照物的不同,可以分为相对于某个控件(Anchor锚)和相对于父控件。具体如下
showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移
showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移
showAtLocation(View parent, int gravity, int x, int y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移
二、我们通过一个例子感受一下,代码的讲解都写在注释里了,所以我就直接上代码和代码的运行结果。
下面是res/layout/activity_main.xml
布局文件:
<LinearLayout 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:orientation="vertical"
android:id="@+id/main"
tools:context="com.example.popupwindow.MainActivity$PlaceholderFragment" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="popup demo"
android:textSize="30sp"
android:gravity="center" />
<Button
android:id="@+id/btn01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="以自己为中心,不偏移" />
<Button
android:id="@+id/btn02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="以自己为中心,有偏移" />
<Button
android:id="@+id/btn03"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="以屏幕中心为参照,不偏移(正中间)" />
<Button
android:id="@+id/btn04"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="以屏幕下方为参照,下方中间" />
</LinearLayout><?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="#0f0"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选择状态:"
android:textColor="@android:color/white"
android:textSize="30sp"/>
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioButton android:text="在线" />
<RadioButton android:text="离线" />
<RadioButton android:text="隐身" />
</RadioGroup>
</LinearLayout>public class MainActivity extends Activity implements View.OnClickListener,
OnCheckedChangeListener {
private Button btn01;
private Button btn02;
private Button btn03;
private Button btn04;
private PopupWindow pop;
// 屏幕的width
private int mScreenWidth;
// 屏幕的height
private int mScreenHeight;
// PopupWindow的width
private int mPopupWindowWidth;
// PopupWindow的height
private int mPopupWindowHeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn01 = (Button) findViewById(R.id.btn01);
btn02 = (Button) findViewById(R.id.btn02);
btn03 = (Button) findViewById(R.id.btn03);
btn04 = (Button) findViewById(R.id.btn04);
btn01.setOnClickListener(this);
btn02.setOnClickListener(this);
btn03.setOnClickListener(this);
btn04.setOnClickListener(this);
initPopupWindow();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
// 相对某个控件的位置(正左下方),无偏移
case R.id.btn01:
getPopupWindowInstance();
pop.showAsDropDown(v);
break;
// 相对某个控件的位置(正左下方),有偏移
case R.id.btn02:
getPopupWindowInstance();
pop.showAsDropDown(v, 50, 50);
break;
// 相对于父控件的位置,无偏移
case R.id.btn03:
getPopupWindowInstance();
pop.showAtLocation(v, Gravity.CENTER, 0, 0);
break;
// 相对于父控件的位置,有偏移
case R.id.btn04:
getPopupWindowInstance();
pop.showAtLocation(v, Gravity.BOTTOM, 0, 50);
break;
}
}
private void initPopupWindow() {
LayoutInflater layoutInflater = LayoutInflater.from(this);
View popupWindow = layoutInflater.inflate(R.layout.popup_window, null);
RadioGroup radioGroup = (RadioGroup) popupWindow.findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(this);
// 创建一个PopupWindow
// 参数1:contentView 指定PopupWindow的内容
// 参数2:width 指定PopupWindow的width
// 参数3:height 指定PopupWindow的height
pop = new PopupWindow(popupWindow, 350, 380);
// 获取屏幕和PopupWindow的width和height
mScreenWidth = getWindowManager().getDefaultDisplay().getWidth();
mScreenWidth = getWindowManager().getDefaultDisplay().getHeight();
mPopupWindowWidth = pop.getWidth();
mPopupWindowHeight = pop.getHeight();
}
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
pop.dismiss();
}
/*
* 获取PopupWindow实例
*/
private void getPopupWindowInstance() {
if (pop != null) {
pop.dismiss();
return;
} else {
initPopupWindow();
}
}
}
原文:http://blog.csdn.net/liguojin1230/article/details/44175581