編輯:關於 Android 程式設計
自訂對話框是開發中很常出現的需求。這裡示範的做法用到了 Android 的 shape 圖形資源,Android 的 drawable 種類相當豐富,後續再另篇整理。本篇的實作可以拆成三個步驟。
一、修改系統預設的 Dialog 樣式(風格與主題)
二、自訂 Dialog 的版面檔
三、封裝一個繼承自 Dialog 的類別(或直接用 Dialog 類別),為了後續重複使用,建議封裝
原始碼下載:https://download.csdn.net/detail/wwj_748/7261031
效果圖:

res/values/styles.xml:
<!-- 對話框主題 -->
<style name="DialogTheme" parent="@android:style/Theme.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
</style>
把 windowBackground 設為透明、隱藏標題列,是讓自訂版面能完全掌控外觀的兩個關鍵設定。若還想讓對話框浮在背景之上、背景變暗,可以再補上 android:windowBackgroundDimEnabled 與 android:backgroundDimAmount。
package com.wwj.custom.dialog;
import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
/**
* 自訂對話框
*/
public class CustomDialog extends Dialog {
private static int default_width = 160; // 預設寬度(dp)
private static int default_height = 120; // 預設高度(dp)
public CustomDialog(Context context) {
super(context);
}
public CustomDialog(Context context, int layout, int style) {
this(context, default_width, default_height, layout, style);
}
public CustomDialog(Context context, int width, int height, int layout, int style) {
super(context, style);
// 設定內容版面
setContentView(layout);
// 設定視窗屬性
Window window = getWindow();
WindowManager.LayoutParams params = window.getAttributes();
// 寬高需換算成實際像素,否則在不同密度裝置上會失真
float density = getDensity(context);
params.width = (int) (width * density);
params.height = (int) (height * density);
params.gravity = Gravity.CENTER;
window.setAttributes(params);
}
/** 取得顯示密度 */
public float getDensity(Context context) {
Resources res = context.getResources();
DisplayMetrics dm = res.getDisplayMetrics();
return dm.density;
}
}
res/layout/dialog_layout.xml:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/dialog_bg"
android:gravity="center"
android:orientation="vertical" >
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="正在執行..." />
</LinearLayout>
版面用到的背景資源 res/drawable/dialog_bg.xml,用 shape 畫出圓角半透明底:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="10dp" />
<solid android:color="#55000000" />
</shape>
package com.wwj.custom.dialog;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomDialog customDialog =
new CustomDialog(this, R.layout.dialog_layout, R.style.DialogTheme);
customDialog.show();
}
}
用 shape 畫圓角矩形背景,比放一張 .9.png 圖片更靈活:改 corners 的半徑就能調圓角,改 solid 的色碼就能調透明度,不需要重新切圖。若想加上邊框,再補一個 <stroke> 即可。
另外,封裝時把寬高設計成 dp 單位並在內部換算成 px,是為了讓同一套程式碼在不同解析度裝置上都有一致的外觀。最後別忘了在 Activity 銷毀時呼叫 dismiss(),否則畫面旋轉或 Activity 被回收時,容易出現 WindowLeaked 的錯誤日誌。
.Net 程序員玩轉 Android 開發(17):Handler 的兩種用法
在 Android 開發中,如果你試圖在一個子線程裡直接給主界面的控件賦值來更新顯示,程序就會拋出異常。這是出於安全考慮——Android 不允許在子線程中直接更新界面控
Android 打飛機遊戲實戰:實現主角及其相關元素(3)
本篇要讓玩家操控的主角登場。主角不仅需要能移動、顯示血量,還要能透過方向鍵靈活走位。先來看看完成後的效果: 建立 Player 類別 主角類別需要管理座標、圖片、血量
Android 仿騰訊 QQ 實現滑動刪除(附完整源碼)
看了許多高手寫的文章,受益良多,也非常欣賞大家的分享精神,於是決定開始寫 Blog,把自己的心得記錄下來與各位交流。 先來看看效果圖: 原本我打算在 ListView
Android HTTPS 請求實戰:自簽憑證的處理與 Volley 整合範例
先理解 HTTPS 憑證這件事 HTTPS 的核心是憑證與數位簽章。簽章通常建立在 RSA 這類非對稱加密演算法之上,而 RSA 之所以安全,來自一個不對稱的計算難題:把