PopupWindow解释

popup:弹出,window:界面。

PopupWindow:用来在Activity上弹出界面。

使用场景是,要在当前Activity上弹出其他界面的时候,需要用到它。

接下来认识下PopupWindow,

PopupWindow的构造函数为

public PopupWindow(View contentView, int width, int height, boolean focusable)

contentView为要显示的view,width和height为宽和高,值为像素值,也可以是MATCHT_PARENT和WRAP_CONTENT。

focusable为是否可以获得焦点,这是一个很重要的参数,也可以通过

public void setFocusable(boolean focusable)

来设置,如果focusable为false,在一个Activity弹出一个PopupWindow,按返回键,由于PopupWindow没有焦点,会直接退出Activity。如果focusable为true,PopupWindow弹出后,所有的触屏和物理按键都有PopupWindows处理。

如果PopupWindow中有Editor的话,focusable要为true。

第一次实现的时候遇到了问题,就是弹出框不会在按下Back键的时候消失,点击弹框外区域也没有正常消失,搜索了一下,都说只要设置背景就好了。然后我就找了个图片,果然弹框能正常dismiss了。

public class CustomSpinnerActivity extends Activity {

	private ImageView spinner_img; // 按钮图片
	private PopupWindow popupWindow;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_spinner);
		
		View popupView = getLayoutInflater().inflate(R.layout.activity_popupwindow_1, null);
		popupWindow = new PopupWindow(popupView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, true);
		
		popupWindow.setTouchable(true);
		popupWindow.setOutsideTouchable(true);
	
		popupWindow.setBackgroundDrawable(new BitmapDrawable(this.getResources(),""));
		
		spinner_img = (ImageView) findViewById(R.id.spinner_arr_img);
		spinner_img.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				showPopuWindows();
				popupWindow.showAsDropDown(v);
			}});
	}
	
	
	protected void showPopuWindows() {
		// TODO Auto-generated method stub
//		PopupWindow popupWindow = new PopupWindow(this, width, height, focusable)
	}
	
	
}

activity_spinner.xml如下:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="20dp"
    android:paddingTop="20dp"
    >
    
    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/frame_color"
        android:orientation="horizontal"
        android:padding="10dp">
        
	    <TextView 
	        android:id="@+id/spinner_content_et"
	        android:layout_width="240dp"
	        android:layout_height="wrap_content"
	        android:background="@null"/>
	    
	    <ImageView 
	        android:id="@+id/spinner_arr_img"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:layout_marginLeft="10dp"
	        android:layout_gravity="center_vertical"
	        android:background="@drawable/down_arrow_2"/>
    </LinearLayout>
</RelativeLayout>
activity_popupwindow_1.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#000000" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:text="@string/app_name" 
        android:textColor="#ffffffff"
        android:layout_centerInParent="true"
        android:gravity="center"/>

</RelativeLayout>








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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值