Android進階2之PopupWindow彈窗(有點懸浮窗的感覺)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
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>
以上只是用來學習之用,拿出來和大家一起分享一下。
有想要原始碼的給我留言。