Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android PopupWindow 彈出層完整實作教學

編輯:關於 Android 程式設計

做 Android 介面時,經常會遇到「點一下按鈕,從螢幕底部滑出一排選項」這種互動。與其硬塞一個 Dialog,不如用 PopupWindow:它能懸浮在目前的 Activity 之上,而且內容可以是任何 View 版面配置,自由度遠比現成的對話框高。

一、先寫好要彈出的版面配置

下面這份 item_popupwindows.xml 做出一個貼齊畫面底部的白色面板,由上到下依序是「拍照」、「從相冊中選取」、「取消」三個項目,中間用細分隔線隔開。

<LinearLayout
    android:id="@+id/ll_popup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="#ffffff"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#ff495a" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/item_popupwindows_camera"
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:background="@drawable/bt_nobgd"
            android:textColor="#585858"
            android:textSize="18sp"
            android:text="拍照" />
    </LinearLayout>

    <TextView
        android:layout_marginLeft="80dp"
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="#f2f2f2" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/item_popupwindows_Photo"
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:background="@drawable/bt_nobgd"
            android:textColor="#585858"
            android:textSize="18sp"
            android:text="從相冊中選取" />
    </LinearLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#f3f3f3" />

    <Button
        android:id="@+id/item_popupwindows_cancel"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:background="@drawable/bt_nobgd"
        android:textColor="#585858"
        android:textSize="18sp"
        android:text="取消" />
</LinearLayout>

二、封裝成一個獨立的 PopupWindow 子類

把 PopupWindow 的細節收進自己的內部類別裡,Activity 只要一行 new PopupWindows(this, v) 就能叫出來,後續要加功能也不必污染 Activity 的生命週期方法。

public class MainActivity extends Activity implements OnClickListener {

    private TextView t_send, t_ragion;
    private Button btn_images;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        t_send = (TextView) findViewById(R.id.t_send);
        btn_images = (Button) findViewById(R.id.btn_images);
        t_ragion = (TextView) findViewById(R.id.t_ragion);

        t_send.setOnClickListener(this);
        btn_images.setOnClickListener(this);
        t_ragion.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_images:
                new PopupWindows(MainActivity.this, v);
                break;
            default:
                break;
        }
    }

    public class PopupWindows extends PopupWindow {

        public PopupWindows(Context mContext, View parent) {
            View view = View.inflate(mContext, R.layout.item_popupwindows, null);
            view.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.fade_ins));

            LinearLayout ll_popup = (LinearLayout) view.findViewById(R.id.ll_popup);
            ll_popup.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.push_bottom_in_2));

            setWidth(LayoutParams.FILL_PARENT);
            setHeight(LayoutParams.FILL_PARENT);
            setBackgroundDrawable(new BitmapDrawable());
            setFocusable(true);
            setOutsideTouchable(true);
            setContentView(view);
            showAtLocation(parent, Gravity.BOTTOM, 0, 0);
            update();

            Button bt1 = (Button) view.findViewById(R.id.item_popupwindows_camera);
            Button bt2 = (Button) view.findViewById(R.id.item_popupwindows_Photo);
            Button bt3 = (Button) view.findViewById(R.id.item_popupwindows_cancel);

            bt1.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    // 指定拍照後照片的存放路徑
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(
                            new File(Environment.getExternalStorageDirectory(), "imageTemp.jpg")));
                    startActivityForResult(intent, 2);
                    dismiss(); // 關閉 Popup 視窗
                }
            });

            bt2.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    Intent intent = new Intent(Intent.ACTION_PICK, null);
                    intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                    startActivityForResult(intent, 1);
                    dismiss();
                }
            });

            bt3.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    dismiss();
                }
            });
        }
    }
}

三、幾個容易忽略的關鍵設定

  • setBackgroundDrawable(new BitmapDrawable()): PopupWindow 若沒有背景,點擊外部區域不會關閉,這個空 Drawable 是必要的「填充物」。
  • setFocusable(true) 與 setOutsideTouchable(true): 前者讓 Popup 搶走焦點、能吃掉返回鍵;後者讓使用者點視窗外的地方也能收到事件。
  • showAtLocation(parent, Gravity.BOTTOM, 0, 0): 決定彈出的位置。改成 Gravity.CENTER 就是置中彈窗。
  • 記得 dismiss(): 每個選項的點擊回呼裡都要自行關閉,PopupWindow 不會幫你收。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved