android中popupwindow佈局的一些小問題

    接觸android也有幾個月了,這幾天用到比較多的popupwindow,把一些遇到的問題和自己的解決方法寫出來。

    關於popupwindow裏的控件自適應長寬,使的界面在不同的手機上看起來都比較美觀:

    佈局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/background_quality"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/text_popup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip"
        android:layout_marginTop="10dip"
        android:paddingBottom="5dip"
        android:text="這是一個popupwindow:"
        android:textColor="#0000AA"
        android:textSize="15dip" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="10dip"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓名:"
            android:textColor="#0000AA" />

         <EditText
            android:layout_width="140dip"
            android:layout_height="wrap_content"
            android:maxLength="3"
            android:text="某某某"
            android:inputType="number" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="學號:"
            android:textColor="#0000AA" />
        
         <EditText
            android:layout_width="140dip"
            android:layout_height="wrap_content"
            android:maxLength="10"
            android:text="5801207021"
            android:inputType="number" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="地址:"
            android:textColor="#0000AA" />

        <EditText
            android:layout_width="140dip"
            android:layout_height="wrap_content"
            android:text="China..." />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="性別:"
            android:textColor="#0000AA" />

        <EditText
            android:layout_width="140dip"
            android:layout_height="wrap_content"
            android:text="男"
            android:selectAllOnFocus="true" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dip"
        android:layout_marginLeft="10dip" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="湊數:"
            android:textColor="#0000AA"
            android:visibility="invisible" />

        <Button
            android:id="@+id/btn_Control_Ok"
            android:layout_width="140dip"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="確認修改"
            android:textColor="#0000AA" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="湊數:"
            android:textColor="#0000AA"
            android:visibility="invisible" />

        <Button
            android:id="@+id/btn_Control_Cancel"
            android:layout_width="140dip"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="關閉"
            android:textColor="#0000AA" />
    </LinearLayout>

</LinearLayout>


效果:

    這裏的各種控件都比較整齊,而且在其他的不同分辨率手機上的效果也是差不多的。這樣的佈局只要是應用wrap_content這個屬性,將自己的一些button、textview等控件放在layout的時候,確定好自己的控件大小,完全可以使layout自由wrap_content,而不用太關心其的大小。而在生成popupwindow的時候,也是直接設置爲wrap_content。

activity代碼:

//Layout
		LayoutInflater layoutInflater = (LayoutInflater) MainActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE);
		View myView = layoutInflater.inflate(R.layout.mypopupwindow, null);
		//設置Layout時的大小,這裏是自適應
		myPopupWindow = new PopupWindow(myView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		btn_PopOK = (Button) myView.findViewById(R.id.btn_Control_Ok);
		btn_PopCancel = (Button) myView.findViewById(R.id.btn_Control_Cancel);

		btn_ShowpWindow = (Button) findViewById(R.id.btn_popupwindow);
		btn_ShowpWindow.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				// TODO Auto-generated method stub
				myPopupWindow.setFocusable(true);//獲得焦點,如果不獲得焦點,則有一些控件比如edittext是不會有輸入法彈出的
				// myPopupWindow.setBackgroundDrawable(new PaintDrawable());//設置背景,加了這句,點擊邊上popupwindow會消失,不知道爲什麼android中就算的xml中設置了背景,這裏要再設置一次,才能達到點擊邊上popupwindow消失的效果
				myPopupWindow.showAtLocation(relativeLayout, Gravity.CENTER, 0, 0);

			}
		});

 有時候,你會在彈出View的時候按鍵變成選中狀態,而點擊了popupwindow外部區域其消失後,想按鍵恢復原來狀態,就需要給popupwindow添加一個消失響應事件,這時可以重寫 popupwindow.setOnDismissListener()這個方法:

pWindow_Jiaoju.setFocusable(true);
		pWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.background));
		pWindow.setOutsideTouchable(true);//有上面這幾句,可以達到點擊pWindow消失的效果
		pWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
		pWindow.setOnDismissListener(new OnDismissListener()
		{
			@Override
			public void onDismiss()
			{
				// TODO Auto-generated method stub
				//...code...	                                                                             }
		});


 

 


 

發佈了22 篇原創文章 · 獲贊 1 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章