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

Android UI 組件進階(2):打造仿 Windows 風格對話框

編輯:關於 Android 程式設計

先祝大家中秋節快樂——相信不少上班的朋友都能放三天假。如果有時間,回家陪陪父母吧,樹欲靜而風不止,子欲養而親不待。

廢話不多說,今天來做一個模仿 Windows 風格的對話框。

效果對比

系統默認的 AlertDialog 長這樣:

今天要做的是這個樣子:

對比之下,第二種帶彩色標題欄、邊框清晰的對話框視覺層次更分明,用戶的注意力更容易被吸引過來。

核心思路並不複雜:不用系統默認樣式,自己寫一個布局,然後用 AlertDialog.Builder.setView() 塞進去。

核心代碼解析

① 按鈕點擊效果:btnexit_selctor.xml

用 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>

② 對話框布局:dialog_win.xml

結構分三塊:彩色標題欄(含關閉按鈕)、內容區、底部按鈕行。

<?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 寬度,實現等分排列。

③ MainActivity.java

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

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