編輯:關於 Android 程式設計
有些特殊場景下,系統 Dialog 提供的樣式無法滿足需求,這時候就得自己定制。以下是一個基礎範例(靈感來自 Stack Overflow),你可以在此之上繼續改造。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:background="#3E80B4"
android:orientation="vertical">
<TextView
android:id="@+id/txt_dia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="Do you really want to exit?"
android:textColor="@android:color/white"
android:textSize="15dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#3E80B4"
android:orientation="horizontal">
<Button
android:id="@+id/btn_yes"
android:layout_width="100dp"
android:layout_height="30dp"
android:background="@android:color/white"
android:text="Yes"
android:textColor="#5DBCD2" />
<Button
android:id="@+id/btn_no"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_marginLeft="5dp"
android:background="@android:color/white"
android:text="No"
android:textColor="#5DBCD2" />
</LinearLayout>
</LinearLayout>
public class CustomDialogClass extends Dialog
implements android.view.View.OnClickListener {
public Activity c;
public Dialog d;
public Button yes, no;
public CustomDialogClass(Activity a) {
super(a);
this.c = a;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_dialog);
yes = (Button) findViewById(R.id.btn_yes);
no = (Button) findViewById(R.id.btn_no);
yes.setOnClickListener(this);
no.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_yes:
c.finish();
break;
case R.id.btn_no:
dismiss();
break;
default:
break;
}
}
}
關鍵點是繼承 Dialog 而非 AlertDialog,這樣就完全擺脫了系統樣式的束縛,版面完全由你的 XML 決定。requestWindowFeature(Window.FEATURE_NO_TITLE) 用來去掉系統標題欄,它必須在 setContentView 之前呼叫。
CustomDialogClass cdd = new CustomDialogClass(Values.this);
cdd.show();
1. 建構子直接呼叫 Activity.finish() 是個糟糕的設計——一個通用 Dialog 元件不該硬編碼「關閉整個頁面」的業務邏輯。應該改成透過回呼介面把使用者選擇傳出去,由呼叫方決定要怎麼處理。
public interface OnDialogButtonClickListener {
void onPositiveClick();
void onNegativeClick();
}
// 使用時
new CustomDialogClass(this, new OnDialogButtonClickListener() {
@Override public void onPositiveClick() { finish(); }
@Override public void onNegativeClick() { }
}).show();
2. 示範中的 onClick 原本在 switch 之後又呼叫了一次 dismiss(),會導致點 Yes 時先 finish 再 dismiss,行為不直觀。改寫後只在 Negative 分支關閉即可。
3. public Activity c 這種寫法暴露了內部欄位,應改為 private 並透過建構子注入。
4. 版面中的 dp 與顏色寫死在 XML 裡,若有多個 Dialog 需要共用風格,建議抽出到 styles.xml 與 colors.xml。
5. 現代寫法建議改用 DialogFragment(或已棄用的 DialogFragment 的替代品)。原因有兩點:一是 Dialog 持有 Activity 的強引用,在螢幕旋轉時容易造成 window leak;二是 DialogFragment 由 FragmentManager 托管,能自動處理重建與生命週期,不必自己操心。
Tomcat + MySQL 自建服務端(2-2):Servlet 工作原理與實戰
上一篇我們對 Servlet 的職責與它在服務端的位置有了大致認識,並在全新的 Workspace 中創建了第一個 Dynamic Web Project 和第一個 Se
用 XUtils 實作照片上傳:客戶端與伺服器端完整流程
做 Android 的朋友大概都用過 XUtils。這篇我們用它做一個照片上傳的 Demo,把客戶端與伺服器端都走一遍,下一篇再從原始碼角度分析 XUtils 的 Htt
Android SlidingMenu 側滑菜單實作
SlidingMenu 是什麼 SlidingMenu 是一種常見的側滑菜單方案,使用者可從畫面左側或右側滑出設定或導覽區。許多應用會使用這種互動方式,讓主內容保持簡潔。
Android Fragment 生命週期詳解
Fragment 的生命週期 每個 Fragment 都有自己的一套生命週期回調方法,並自行處理用戶輸入事件。對應的生命週期可參考下圖: 創建 Fragment 要創建