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

Android 氣泡提示視窗的實作方法

編輯:關於 Android 程式設計

本文整理一個氣泡視窗的實作:一個帶背景插圖的小視窗,裡面有一段說明文字與一個「我知道了」的連結,點擊後關閉。這種效果常用於新手指引或異常提示。

版面結構

氣泡由一個背景圖加上兩個 TextView 組成:一個放內容,一個放「我知道了」。氣泡的外觀其實就是這個 layout 的背景,因此只要換掉 android:background 就能替換成不同造型的氣泡圖。

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:background="@drawable/pub_pop_bg4"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/bubble_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18dp"
        android:textColor="#000" />

    <!-- 我知道了 -->
    <TextView
        android:id="@+id/bubble_btn"
        android:gravity="right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/bubble_text"
        android:textSize="20dp"
        android:textColorHighlight="#2151E0"
        android:text=""
        android:textColor="#1A75B9" />
</RelativeLayout>

Activity 程式碼

public class TestBubbleActivity extends Activity {

    /** 螢幕寬高 */
    private static int SCREEN_WIDTH = 0;
    private static int SCREEN_HEIGHT = 0;

    private TextView tvBubContent = null;   // 氣泡內容
    private TextView tvKnow = null;         // 「我知道了」
    private View bubbleView = null;         // 氣泡整體
    private Dialog bubbleAlert = null;      // 承載氣泡的 Dialog

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        getDimension();

        bubbleView = getLayoutInflater().inflate(R.layout.overlay_pop, null);

        tvKnow = (TextView) bubbleView.findViewById(R.id.bubble_btn);
        tvKnow.setText(Html.fromHtml("<u>" + "我知道了" + "</u>"));

        tvBubContent = (TextView) bubbleView.findViewById(R.id.bubble_text);
        tvBubContent.setText("上次程序異常退出,正在傳輸歷史數據...");

        tvKnow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bubbleAlert.cancel();
            }
        });

        int tmpWidth = SCREEN_WIDTH / 5 * 3;
        int tmpHeight = SCREEN_HEIGHT / 8;

        tvKnow.setMinWidth(tmpWidth);
        tvBubContent.setMaxWidth(tmpWidth);

        // 以自訂樣式建立 Dialog
        bubbleAlert = new Dialog(this, R.style.bubble_dialog);
        Window win = bubbleAlert.getWindow();
        LayoutParams params = win.getAttributes();
        params.x = -(SCREEN_WIDTH / 8);   // 水平偏移
        params.y = -tmpHeight;            // 垂直偏移
        params.width = tmpWidth;
        win.setAttributes(params);

        bubbleAlert.setCancelable(false);
        bubbleAlert.setContentView(bubbleView);
        bubbleAlert.show();
    }

    /** 取得螢幕尺寸 */
    private void getDimension() {
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        SCREEN_WIDTH = dm.widthPixels;
        SCREEN_HEIGHT = dm.heightPixels;
    }
}

幾個可以調整的地方

位置全靠 LayoutParams 的 x、y 偏移控制,因此要讓氣泡指向某個按鈕,只要把該按鈕的螢幕座標減掉一半寬度帶入即可。另外 setCancelable(false) 讓使用者必須按下「我知道了」才能關閉,適合強提示;若希望點外部也能關掉,把它改為 true 即可。最後,R.style.bubble_dialog 通常會把視窗背景設為透明並關閉陰影,這是氣泡看起來「浮」在畫面上的關鍵設定。

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