Android PopupWindow

1. activity_main.xml


<span style="font-family:Microsoft YaHei;font-size:12px;"><RelativeLayout 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"
    tools:context="com.example.popupwindow.MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="点击弹出PopupWindow" />

</RelativeLayout></span>

2.定义popupWindow视图

新建popup_window.xml

<span style="font-family:Microsoft YaHei;font-size:12px;"><?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"
    android:gravity="center"
    android:paddingBottom="8dp" >

    <Button
        android:id="@+id/textView2"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="取消" />

</RelativeLayout></span>


3.MainActivity代码:


<span style="font-family:Microsoft YaHei;font-size:12px;">public class MainActivity extends Activity {
	private Button button;
	PopupWindow popupWindow;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button = (Button) findViewById(R.id.button1);
		intiPopup();// 初始化PopupWindow
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
                                //设置PopupWind的显示位置。
				<span style="color:#ff0000;">popupWindow.showAtLocation(button, Gravity.NO_GRAVITY,
						(int) button.getX(), (int) button.getY());</span>
			}
		});
	}

	private void intiPopup() {
		// 加载PopupWindow的布局文件
		View contentView = LayoutInflater.from(getApplicationContext())
				.inflate(R.layout.popup_window, null, false);

		// 声明一个对话框
		popupWindow = new PopupWindow(null, LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT, true);

		// 为自定义的对话框设置自定义布局
		popupWindow.setContentView(contentView);
showAsDropDown(anchor, xoff, yoff);
		// 需要设置一下此参数,点击外边可消失
		popupWindow.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.quespage_center_normal_bg));
		// 设置点击窗口外边窗口消失
		popupWindow.setOutsideTouchable(true);
		// 设置此参数获得焦点,否则无法点击
		popupWindow.setFocusable(true);

		Button buttonC = (Button) contentView.findViewById(R.id.textView2);
		buttonC.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				popupWindow.dismiss();// 关闭PopupWindow
			}
		});
	}

}</span>

关于popupWindow显示位置的详解:


1.showAsDropDown(anchor);


以触发弹出窗的view为基准,出现在view的正下方,弹出的pop_view左上角正对view的左下角 偏移量默认为0,0

2.showAsDropDown(anchor, xoff, yoff);


有参数的话,就是一view的左下角进行偏移,xoff正的向左,负的向右. yoff没测,也应该是正的向下,负的向上

3.showAtLocation(parent, gravity, x, y);

parent:传你当前Layout的id; 

gravity:Gravity.BOTTOM(以屏幕左下角为参照)... 偏移量会以它为基准点 当x y为0,0是出现在底部居中


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值