Android UI:PopupWindow基本学习

基本使用

什么是PopupWindow?

     就是点击某个控件,弹出个view,弹出的view就是PopupWindow
效果图:点击button,弹出PopWindow,里面又有4个button,点击第一个button,弹出toast。


popwindow对应的布局

<?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/btn_pop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn01"
        android:textSize="20px" />
      
      <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn02"
        android:textSize="20px" />
      <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn03"
        android:textSize="20px" />
      <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn0"
        android:textSize="20px" />

</LinearLayout>

3 代码

package com.examp.popupwindowdemo;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn=(Button) findViewById(R.id.btn);
        btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//打开popupwindow
				showPopupWindow(v);
			}
		});
    }


    protected void showPopupWindow(View v) {
		View contentView = View.inflate(getApplicationContext(), R.layout.popwindow, null);
		PopupWindow popupWindow = new PopupWindow(contentView, 
				LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		popupWindow.setTouchable(true);//popupwindow可点击
		popupWindow.setOutsideTouchable(true);//popupwindow以外的地方可点击,使其调用dismiss()方法,不设置这个点击popupwindow以外的区域pop不会消失
		popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//必须设置Drawable背景(2中方法)
//		popupWindow.setBackgroundDrawable(new  BitmapDrawable());
		Button btn_pop=(Button) contentView.findViewById(R.id.btn_pop);
		btn_pop.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Toast.makeText(getApplicationContext(), "pop弹出toast", Toast.LENGTH_LONG).show();
			}
		});
		popupWindow.showAsDropDown(v);
	}

}

注意

必须设置BackgroundDrawable

必须给popupWindow设置BackgroundDrawable,否则setFocusable(true)无效
popupWindow.setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
//popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//(2中方法)
//popupWindow.setBackgroundDrawable(new  BitmapDrawable());

设置popupwindow的显示位置

第一种:

int[] locations = new int[2];
btn.getLocationInWindow(locations);//Gravity.TOP|Gravity.LEFT
popupWindow.showAtLocation(btn,Gravity.TOP, locations[0], locations[1]);第二种:
//方式二:显示在View下方
//没有偏移量
popupWindow.showAsDropDown(view);
//有偏移量:后2个参数是x y轴偏移量
popupWindow.showAsDropDown(view, 100, 0);

创建PopupWindow的工具类

public class WindowUtils {

	/**
	 * 显示弹出视图
	 * @param context
	 * @param positionView
	 * @param view
	 * @param x
	 * @param y
	 * @return
	 */
	public static PopupWindow showPopWindow(Context context, View positionView, View view, int x, int y) {

		// 创建 PopupWindow
		// PopupWindow popwindow=new PopupWindow(视图对象 , 宽, 高);
		PopupWindow popwindow = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		
		//在视图对象 以外的范围 点击关闭
		popwindow.setOutsideTouchable(true);//自动关闭
		
		popwindow.setFocusable(true);//响应返回键关闭1.聚集 2.背景
		popwindow.setBackgroundDrawable(new ColorDrawable(android.R.color.transparent));
		// 指定控件做参考 显示出视图
		// popwindow.showAtLocation(参考视图, 坐标位置, 偏移x,偏移y)
		// int [] location =new int[2];
		// positionView.getLocationOnScreen(location);//获取视图在屏幕中的坐标
		popwindow.showAtLocation(positionView, Gravity.LEFT | Gravity.TOP, x, y);
		
		return popwindow;
	}
}

dismiss()无效的原因

添加如下代码

popupWindow.setTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);

 
 

其它弹框

其他弹框 Dialog

PopupWindow Spinner

123

11111

3333333333

222222222

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值