Android PopupWindow的使用

效果图:

在onCreate()中,初始化

	
		LayoutInflater inflater = LayoutInflater.from(this);
		View view = inflater.inflate(R.layout.mypopwindow, null);
		tv_1 = (TextView) view.findViewById(R.id.tv_1);
		tv_2 = (TextView) view.findViewById(R.id.tv_2);
		tv_1.setOnClickListener(this);
		tv_2.setOnClickListener(this);
	    popupWindow = new PopupWindow(view,LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
	    popupWindow.setBackgroundDrawable(new BitmapDrawable());  
	    popupWindow.setOutsideTouchable(true);
	    popupWindow.setFocusable(true);		
布局文件 mypopwindow中,设置布局文件。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="200dp"
    android:layout_height="wrap_content" >
    <LinearLayout
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:orientation="vertical" >
        <TextView
            android:id="@+id/tv_1"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:gravity="center"
            android:text="我的财富"
            android:textColor="@color/black"
            android:textSize="15sp" />
        <View
            android:layout_width="fill_parent"
            android:layout_height="0.5dp"
            android:background="@color/gray_line" />
        <TextView
            android:id="@+id/tv_2"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:gravity="center"
            android:text="我的红包"
            android:textColor="@color/black"
            android:textSize="15sp" />
    </LinearLayout>
</RelativeLayout>

显现出弹窗,

case R.id.btn_choose:
	//显现出弹窗
	popupWindow.showAsDropDown(v);  
break;

隐藏弹窗,popupWindow.dismiss();


另一种方式

package com.duguang.baseanimation.ui.customview.popwindow;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.duguang.baseanimation.R;
import com.duguang.baseanimation.ui.base.BaseActivity;

/**
 * 自定义PopWindow 主页面
 * @author Administrator
 *
 */
public class PoupWindowMainActivity extends BaseActivity {

	private PopupWindow popupWindow;

	private ListView lv_group;

	private View view;

	private View top_title;

	private TextView tvtitle;

	private List<String> groups;

	/**
	 * 显示
	 * 
	 * @param parent
	 */
	private void showWindow(View parent) {

		if (popupWindow == null) {
			LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			view = layoutInflater.inflate(R.layout.activity_custom_popwindow_group_list, null);

			lv_group = (ListView) view.findViewById(R.id.lvGroup);
			// 加载数据
			groups = new ArrayList<String>();
			groups.add("我的微博");
			groups.add("好友");
			groups.add("亲人");
			groups.add("陌生人");

			GroupAdapter groupAdapter = new GroupAdapter(this, groups);
			lv_group.setAdapter(groupAdapter);
			// 创建一个PopuWidow对象
			popupWindow = new PopupWindow(view, 400, 450);
		}

		// 使其聚集
		popupWindow.setFocusable(true);
		// 设置允许在外点击消失
		popupWindow.setOutsideTouchable(true);

		// 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
		// 显示的位置为:屏幕的宽度的一半-PopupWindow的高度的一半
		int xPos = windowManager.getDefaultDisplay().getWidth() / 2
				- popupWindow.getWidth() / 2;

		Log.i("coder", "windowManager.getDefaultDisplay().getWidth()/2:"
				+ windowManager.getDefaultDisplay().getWidth() / 2);
		//
		Log.i("coder", "popupWindow.getWidth()/2:" + popupWindow.getWidth() / 2);

		Log.i("coder", "xPos:" + xPos);

		popupWindow.showAsDropDown(parent, xPos, 0);

		lv_group.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int position, long id) {

				Toast.makeText(PoupWindowMainActivity.this,
						"groups.get(position)" + groups.get(position), 1000)
						.show();

				if (popupWindow != null) {
					popupWindow.dismiss();
				}
			}
		});
	}

	@Override
	public void setView() {
		setContentView(R.layout.activity_custom_popwindow_main);

	}

	@Override
	public void initView() {
		top_title = this.findViewById(R.id.top_title);
		tvtitle = (TextView) top_title.findViewById(R.id.tvtitle);
		tvtitle.setText("做一个低调的码农");
		
	}

	@Override
	public void setListener() {
		tvtitle.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				showWindow(v);
			}
		});
		
	}
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值