編輯:關於 Android 程式設計
寫 Android 應用幾乎一定會碰到「系統預設 Dialog 樣式跟自家設計稿搭不上」的狀況。還好自訂 Dialog 只要三個步驟:
styles.xml 中定義對應的 Theme。這個範例的效果是:中央彈出一張圖片(例如 QR Code),點擊 Dialog 以外的區域自動關閉。
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;
/**
* 中央顯示圖片、點擊外部區域關閉的自訂 Dialog
*/
public class CustomPopDialog2 extends Dialog {
public CustomPopDialog2(Context context) {
super(context);
}
public CustomPopDialog2(Context context, int theme) {
super(context, theme);
}
public static class Builder {
private Context context;
private Bitmap image;
public Builder(Context context) {
this.context = context;
}
public Bitmap getImage() {
return image;
}
public void setImage(Bitmap image) {
this.image = image;
}
public CustomPopDialog2 create() {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final CustomPopDialog2 dialog = new CustomPopDialog2(context, R.style.Dialog);
View layout = inflater.inflate(R.layout.dialog_share_qrcode, null);
dialog.addContentView(layout, new LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
dialog.setContentView(layout);
ImageView img = (ImageView) layout.findViewById(R.id.img_qrcode);
img.setImageBitmap(getImage());
return dialog;
}
}
}
這裡的關鍵是 create():把自訂版面 inflate 出來、塞進 Dialog、再把資料填進去。用 Builder 模式包裝,呼叫端寫起來會乾淨很多。
<!-- res/layout/dialog_share_qrcode.xml -->
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/img_qrcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="二維碼" />
</LinearLayout>
Theme 的重點是把系統 Dialog 的灰色背景與標題列拿掉,讓自訂版面完全接管:
<!-- res/values/styles.xml -->
<style name="Dialog" parent="android:style/Theme.Dialog">
<item name="android:background">#00000000</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
</style>
Bitmap bitmap = xxxxx; // 取得要顯示的圖片,也可替換成其他參數
CustomPopDialog2.Builder dialogBuild = new CustomPopDialog2.Builder(context);
dialogBuild.setImage(bitmap);
CustomPopDialog2 dialog = dialogBuild.create();
dialog.setCanceledOnTouchOutside(true); // 點擊外部區域關閉
dialog.show();

onDestroy() 或畫面離開時呼叫 dismiss(),避免 Activity 已銷毀卻仍持有 Dialog 造成 WindowLeaked。AndroidManifest 或 Window 上處理 adjustResize / adjustPan,否則鍵盤會把輸入框蓋住。Dialog,不如直接用 DialogFragment(或 DialogFragmentCompat)——它會自動跟著 Fragment 生命週期管理,旋轉螢幕與背景重設時也不會出錯。BottomSheetDialog 或 PopupWindow 往往比自訂 Dialog 更省事。
Android 螢幕適配實戰:布局層級該注意的那些事(下篇)
上一篇我們把適配的相關概念梳理了一遍,這篇接著談:真正動手寫布局時,有哪些地方必須留心。 一、盡量不要寫死具體尺寸 要讓同一份布局適應各種尺寸的螢幕,前提是不把尺寸寫
Android 開發系列(二):動手做一個短信發送器
本文的目標很明確:做一個能用的短信發送器,包含收件號碼輸入、正文輸入和發送按鈕。 界面效果大致如下: 一、申明發送短信的權限 發送短信屬於敏感操作,必須先在 Andr
Android 開發中常見的資料儲存方式詳解
在 Android 中,資料的儲存大致可分為兩種方式:1、直接以檔案形式儲存在目錄中; 2、以資料庫形式儲存。而「以檔案儲存」又可再細分為兩種:1、產生 .txt 檔;
Android 手寫塗鴉 Demo:自訂 View 保留筆跡軌跡
想在圖片上自由作畫?這裡寫了一個可直接運行的 demo 供參考,重點在於自訂 View 如何保留手寫軌跡。 一、專案結構 二、自訂 GameView 核心思路是:載