這篇文章主要講解了Android自定義Dialog的用法,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。

Android開發(fā)過程中,常常會遇到一些需求場景——在界面上彈出一個彈框,對用戶進行提醒并讓用戶進行某些選擇性的操作,
如退出登錄時的彈窗,讓用戶選擇“退出”還是“取消”等操作。
Android系統(tǒng)提供了Dialog類,以及Dialog的子類,常見如AlertDialog來實現(xiàn)此類功能。
一般情況下,利用Android提供的Dialog及其子類能夠滿足多數(shù)此類需求,然而,其不足之處體現(xiàn)在:
1. 基于Android提供的Dialog及其子類樣式單一,風格上與App本身風格可能不太協(xié)調;
2. Dialog彈窗在布局和功能上有所限制,有時不一定能滿足實際的業(yè)務需求。
本文將通過在Dialog基礎上構建自定義的Dialog彈窗,以最常見的確認彈框為例。
本樣式相對比較簡單:上面有一個彈框標題(提示語),下面左右分別是“確認”和“取消”按鈕,當用戶點擊“確認”按鈕時,彈框執(zhí)行
相應的確認邏輯,當點擊“取消”按鈕時,執(zhí)行相應的取消邏輯。
首先,自定義彈框樣式:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/dialog_bg"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingTop="14dp"
android:textColor="@color/login_hint"
android:textSize="@dimen/text_size_18" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="14dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="30dp" >
<TextView
android:id="@+id/confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:background="@drawable/btn_confirm_selector"
android:gravity="center"
android:textColor="@color/white"
android:textSize="@dimen/text_size_16" />
<TextView
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:background="@drawable/btn_cancel_selector"
android:gravity="center"
android:textColor="@color/login_hint"
android:textSize="@dimen/text_size_16" />
</LinearLayout>
</LinearLayout>
本文名稱:Android自定義Dialog的用法-創(chuàng)新互聯(lián)
本文地址:http://chinadenli.net/article38/pispp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供App設計、手機網(wǎng)站建設、標簽優(yōu)化、品牌網(wǎng)站設計、品牌網(wǎng)站建設、云服務器
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內容