編輯:關於 Android 程式設計
先祝大家中秋節快樂——相信不少上班的朋友都能放三天假。如果有時間,回家陪陪父母吧,樹欲靜而風不止,子欲養而親不待。
廢話不多說,今天來做一個模仿 Windows 風格的對話框。
系統默認的 AlertDialog 長這樣:

今天要做的是這個樣子:

對比之下,第二種帶彩色標題欄、邊框清晰的對話框視覺層次更分明,用戶的注意力更容易被吸引過來。
核心思路並不複雜:不用系統默認樣式,自己寫一個布局,然後用 AlertDialog.Builder.setView() 塞進去。
用 selector 讓關閉按鈕在按下時換一張圖,產生反饋感:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android" >
<item android:state_pressed="false" android:drawable="@drawable/btnexit"/>
<item android:state_pressed="true" android:drawable="@drawable/btnexit_s"/>
</selector>
結構分三塊:彩色標題欄(含關閉按鈕)、內容區、底部按鈕行。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- 標題欄 -->
<RelativeLayout
android:id="@+id/titlelayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#53CC66"
android:padding="5dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提示信息"
android:textColor="#ffffff"
android:textSize="15sp" />
<ImageButton
android:id="@+id/btncancle"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/btnexit_selctor"
android:layout_alignParentRight="true" />
</RelativeLayout>
<!-- 內容區 -->
<LinearLayout
android:id="@+id/detaillayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/titlelayout"
android:layout_marginLeft="30dp"
android:layout_marginTop="22dp"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="仿Windows對話框Demo"
android:textColor="#04AEDA"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="作者:Coder-pig"
android:textColor="#04AEDA"
android:textSize="18sp" />
</LinearLayout>
<!-- 底部按鈕行 -->
<LinearLayout
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/detaillayout" >
<Button
android:id="@+id/btnblog"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/btnpress_selctor"
android:text="訪問博客"
android:layout_margin="5dp"
android:layout_weight="1"
android:textColor="#ffffff"
android:textSize="20sp" />
<Button
android:id="@+id/btnclose"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/btnpress_selctor"
android:text="關閉"
android:layout_margin="5dp"
android:layout_weight="1"
android:textColor="#ffffff"
android:textSize="20sp" />
</LinearLayout>
</RelativeLayout>
兩個細節值得注意:標題欄用 RelativeLayout 讓關閉按鈕 alignParentRight,內容區>Left 對齊;底部兩個按鈕都用 layout_weight="1" 配合 match_parent 寬度,實現等分排列。
public class MainActivity extends Activity {
private Button btnshow;
private View dialogView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnshow = (Button) findViewById(R.id.btnshow);
btnshow.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Builder builder = myBuilder(MainActivity.this);
final AlertDialog dialog = builder.show();
// 設置點擊對話框外部時不消失
dialog.setCanceledOnTouchOutside(false);
// ① 右上角關閉按鈕
ImageButton imgcancle = (ImageButton) dialogView.findViewById(R.id.btncancle);
imgcancle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
// ② 訪問博客按鈕
Button btnblob = (Button) dialogView.findViewById(R.id.btnblog);
btnblob.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "訪問博客",
Toast.LENGTH_SHORT).show();
Uri uri = Uri.parse("https://blog.csdn.net/coder_pig");
startActivity(new Intent(Intent.ACTION_VIEW, uri));
dialog.dismiss();
}
});
// ③ 關閉按鈕
Button btnclose = (Button) dialogView.findViewById(R.id.btnclose);
btnclose.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
}
// 設置對話框顯示的自定義視圖
private Builder myBuilder(Context context) {
LayoutInflater inflater = MainActivity.this.getLayoutInflater();
AlertDialog.Builder builder = new AlertDialog.Builder(context);
dialogView = inflater.inflate(R.layout.dialog_win, null);
return builder.setView(dialogView);
}
});
}
}
setView() 是整個方案的支點。把自定義布局 inflate 出來交給 Builder,對話框的外框(遮罩、進入動畫)仍由系統提供,好處是不用自己處理這些細節。dialogView.findViewById() 而不是 findViewById()。這是新手最常犯的錯——自定義布局的控件並不在 Activity 的視圖樹裡,用 Activity 的 findViewById() 會返回 null。setCanceledOnTouchOutside(false) 防止用戶誤觸對話框外部導致內容丟失,對於承載重要信息的對話框是必要的。setView(),也可以在 styles.xml 裡定義一個繼承自 AlertDialog 的主題,覆寫 android:windowBackground、android:windowTitleBackgroundStyle 等屬性,這樣能保留系統的標題/按鈕結構,改動更小。https://pan.baidu.com/s/1jGone5o
RxAndroid 快速入門:從 RxJava 概念到實戰範例
一、RxJava 是什麼 官方對 RxJava 的定義是:一個在 Java 虛擬機上,以可觀察序列來組裝非同步、事件驅動程式的函式庫。一句話概括就是「非同步」——換句話說
Android 開發環境建置總整理(Eclipse 篇)
開始學 Android 開發,第一道關卡就是環境建置。這類文章網路上很多,本文想補充的是兩個常被忽略的環節:ADT 的下載技巧,以及 Eclipse 的配色調整。 一、
Android 應用線上更新機制解析
線上更新的基本概念 許多應用程式在啟動時會檢查是否有新版本。常見流程是向伺服器請求版本資訊,與本機版本比較,若需要更新則提示使用者,並下載新的 APK。 下圖為專案結
動手讀懂 Google 官方範例:Activity 生命週期其實可以「看」見
前幾天終於把 Google Training 裡的 Manage the Activity Lifecycle 這一節認真看完。過去翻書、看影片,對生命週期各個回呼該做什