天天看點

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>
           
PopupWindow實作android自定義彈出框口
PopupWindow實作android自定義彈出框口
PopupWindow實作android自定義彈出框口
PopupWindow實作android自定義彈出框口
PopupWindow實作android自定義彈出框口