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

Android 對話框練習:Dialog 與 PopupWindow 兩種實作對比

編輯:關於 Android 開發

Android 對話框練習:Dialog 與 PopupWindow 兩種實作對比


這篇練習用兩種方式做出「對話框」效果,分別是傳統的 Dialog,以及更靈活的 PopupWindow。

做法一:Dialog

Dialog 需要搭配自訂主題才能拿掉預設的白色背景方塊,做法是讓 windowBackground 設為 @null:

package com.example.tsh;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Dialog 實現對話框
        Dialog dialog = new Dialog(this, R.style.Theme_Tsh_Dialog);
        dialog.setContentView(R.layout.activity_two);
        dialog.show();
    }
}

對應的主題定義:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="https://schemas.android.com/apk/res/android">
    <!-- 自定義對話框主題 -->
    <style name="Theme.Tsh.Dialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowBackground">@null</item>
    </style>
</resources>

做法二:PopupWindow

PopupWindow 的好處是可以掛在某個 View 下方(像下拉選單),還能設定點擊外部自動關閉:

package com.example.tsh;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button dropDown = (Button) findViewById(R.id.bt_dropdown);
        dropDown.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                View anchor = View.inflate(MainActivity.this, R.layout.activity_two, null);
                final PopupWindow popupWindow = new PopupWindow(anchor,
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
                // 點擊空白隱藏
                popupWindow.setTouchable(true);
                popupWindow.setTouchInterceptor(new OnTouchListener() {
                    @Override
                    public boolean onTouch(View arg0, MotionEvent arg1) {
                        popupWindow.dismiss();
                        return false;
                    }
                });
                popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_launcher));
                popupWindow.showAsDropDown(arg0);
            }
        });
    }
}

主布局很簡單,一顆觸發按鈕即可:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="第一個界面" />

    <Button
        android:id="@+id/bt_dropdown"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="顯示下拉" />
</LinearLayout>

兩者差別在於:Dialog 適合置中的模態提示,PopupWindow 則更適合依附於某個控件的位置彈出,靈活度更高。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved