PopupWindow实现android自定义弹出框口

android开发中我们要实现很多自定义的弹出窗口,我们可以使用PopupWindow来实现
public class SelectDialog extends Activity implements OnClickListener {	
	
	private PopupWindow popupWindow2 = null;//  PopupWindow在android.widget包下,弹出窗口的形式展示。官方文档对该控件的描述是:“一个弹出窗口控件,可以用来显示任意视图(View),而且会浮动在当前 活动(activity)的顶部”。PopupWindow可以让我们实现多种自定义控件,例如:menu、alertdialog等弹窗似的View 
	private LayoutInflater layoutInflater2;//为了获取res 下的layout的xml文件
	private View popView2;
	private ImageView gengDuo;//更多下拉条

		@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_activity);
      gengDuo = (ImageView) v.findViewById(R.id.geng_duo);
     }

 public void showSelect(View anchor) {//anchor 想让该弹出框在那个组件下显示 就传哪个组件
		Rect frame = new Rect();
		getActivity().getWindow().getDecorView()
				.getWindowVisibleDisplayFrame(frame);
		int i = 0;
		int j = 0;
		int[] location = { i, j };
		anchor.getLocationOnScreen(location);
		layoutInflater2 = LayoutInflater.from(this);
		popView2 = layoutInflater2.inflate(R.layout.layout_pop_type, null);
		popView2.setFocusableInTouchMode(true);
		popView2.setFocusable(true);
		LinearLayout layMScan = (LinearLayout) popView2
				.findViewById(R.id.lay_m_scan);
		LinearLayout layMLoc = (LinearLayout) popView2
				.findViewById(R.id.lay_m_loc);
		layMScan.setOnClickListener(this);
		layMLoc.setOnClickListener(this);
		popupWindow2 = new PopupWindow(popView2,
				android.view.ViewGroup.LayoutParams.WRAP_CONTENT/*
																 * DipPxUtil.dip2px
																 * (context,
																 * 126)
																 */,
				android.view.ViewGroup.LayoutParams.WRAP_CONTENT, true);
		ColorDrawable dw = new ColorDrawable(999999);
		popupWindow2.setBackgroundDrawable(dw); //如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
		popupWindow2.setFocusable(true);
		popupWindow2.setOutsideTouchable(true);//点击弹出框外任意一区域弹出框消失
		popupWindow2.update();
		popupWindow2.showAsDropDown(anchor, anchor.getWidth(), 0);

	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.geng_duo:// 点击更多时显示下拉列表
			showSelect(v); 设置按钮的点击事件
			break;
	 	case R.id.lay_m_scan:// 扫描
		 
			break;
		case R.id.lay_m_loc:// 定位
		 
			break;
		default:
			break;
		}
	}

}

layout_pop_type.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="right"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="116dp"
        android:layout_height="100dp"
        android:layout_gravity="center|right"
        android:gravity="left|center"
        android:background="@drawable/buttom_function_bg"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/lay_m_scan"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/type_m_scan"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_marginRight="8dp"
                android:background="@drawable/m_scan" />

            <TextView
                android:id="@+id/type_mscan_tx"
                android:layout_width="wrap_content"
                android:layout_height="25dp"
                android:layout_marginRight="10dp"
                android:gravity="center"
                android:text="扫描"
                android:textColor="#999999"
                android:textSize="16sp" />
        </LinearLayout>

     

        <LinearLayout
            android:id="@+id/lay_m_loc"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:orientation="horizontal"
            android:paddingTop="10dp" >

            <ImageView
                android:id="@+id/type_m_loc"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_marginRight="8dp"
                android:background="@drawable/m_loc" />

            <TextView
                android:id="@+id/type_mloc_tx"
                android:layout_width="wrap_content"
                android:layout_height="25dp"
                android:layout_marginRight="10dp"
                android:gravity="center"
                android:text="定位"
                android:textColor="#999999"
                android:textSize="16sp" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值