Android中的PopWindow控件

什么事PopWindow呢?其实很常见的,就是一个简单界面本来不在屏幕中,通过点击事件就会在所点击的控件例如Button旁边显示出来,通过点击屏幕周边可以再次隐藏,以下我们以一个分享的例子做一个简单的实现。

布局文件如下

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.briup.popwindow.MainActivity" >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/b" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_centerHorizontal="true"
        android:background="#ffffff"
        android:onClick="ShowPopWindow"
        android:text="分享" />

</RelativeLayout>
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:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/ic_launcher"
        android:gravity="center"
        android:text="分享给好友" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/ic_launcher"
        android:gravity="center"
        android:text="分享到朋友圈" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/ic_launcher"
        android:gravity="center"
        android:text="分享到空间" />

</LinearLayout>
Activity类文件如下

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.PopupWindow;
import android.widget.RelativeLayout.LayoutParams;

public class MainActivity extends Activity {
	private PopupWindow pop;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
	
	public void ShowPopWindow(View v){
		getPopWindow();
		pop.showAsDropDown(v,100,100);//展示PopWindow并设置距离按钮的位置
	}

	private void getPopWindow() {
		if(pop!=null){
			pop.dismiss();
			return;
		}else{
			initPopWindow();
		}
	}

	private void initPopWindow() {
		View contentView = View.inflate(this, R.layout.pop_contentview, null);
		pop = new PopupWindow(contentView,//弹出界面的引用 
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT,//界面的宽高属性
				true);//是否可点击
		contentView.setOnTouchListener(new OnTouchListener() {
			//设定点击收回效果
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(pop!=null&&pop.isShowing()){//如果pop已存在并且在显示
					pop.dismiss();
					//pop=null;
				}
				return false;//事件处理后返回false
			}
		});
	}
}
点击按钮后效果如下

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值