Android進階2之PopupWindow彈窗(有點懸浮窗的感覺)
PopupWindow是一個可以用來顯示一個任意的視圖的彈出窗口,他需要完全依賴layout布局。
它沒什么界面,在彈出的窗口中完全顯示布局中的控件。
上面兩個美女頭就是彈窗PopupWindow顯示的內容。是兩個Button。
具體實現:
注意:那三個Button不能和普通的Button一樣通過findViewById()方法獲得,必須首先說的Button所在的視圖,View popview = layoutInflater.inflate(R.layout.poplayout, null);
我的Button在poplayout.xml中。最后通過button1 = (Button) popview.findViewById(R.id.button1)獲得。
另外一點就是:不要在oncreate()中獲得Button,而是像我一樣在onclick方法下獲得,和popupwindow一起。這一點不一定正確。
為什么要提呢?因為我在oncreate()中獲得Button時,button的點擊事件不能用,我也不是很清楚。那位大牛要是知道的話,可以告訴我一下。
showAtLocation(findViewById(R.id.edit_layout), Gravity.BOTTOM,0, 0);
設置彈窗的位置:
第一個參數是彈窗父控件的布局;
第二個參數是位置如左,右,上部,下部等;
第三個參數是X方向的偏移量;
第四個參數是Y方向的偏移量。
package xiaosi.popwindow; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.WindowManager.LayoutParams; import android.widget.Button; import android.widget.PopupWindow; import android.widget.Toast; public class PopwindowActivity extends Activity implements OnClickListener { /** Called when the activity is first created. */ private Button button = null; private Button button1 = null; private Button button2 = null; private Button button3 = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button = (Button) findViewById(R.id.button); button.setOnClickListener(this); } public void onClick(View arg0) { if (arg0.getId() == R.id.button) { LayoutInflater layoutInflater = (LayoutInflater) (PopwindowActivity.this) .getSystemService(LAYOUT_INFLATER_SERVICE); // 獲取自定義布局文件poplayout.xml的視圖 View popview = layoutInflater.inflate(R.layout.poplayout, null); PopupWindow popWindow = new PopupWindow(popview, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); //規定彈窗的位置 popWindow.showAtLocation(findViewById(R.id.main), Gravity.BOTTOM, 0, 0); //PopupWindow里的兩個Button button1 = (Button) popview.findViewById(R.id.button1); button1.setOnClickListener(this); button2 = (Button) popview.findViewById(R.id.button2); button2.setOnClickListener(this); } else if (arg0.getId() == R.id.button1) { Toast.makeText(PopwindowActivity.this, "button1", Toast.LENGTH_LONG) .show(); } else if (arg0.getId() == R.id.button2) { Toast.makeText(PopwindowActivity.this, "button2", Toast.LENGTH_LONG) .show(); } } }poplayout.xml
<?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:gravity="center_horizontal" android:orientation="horizontal" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/colorframe_1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/colorframe_3" /> </LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="@drawable/background"> <Button android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="點擊查看效果" /> <ImageView android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/a" /> </LinearLayout>轉自:http://blog.csdn.net/sjf0115/article/details/7339914
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!