Android中自定義對(duì)話框(Dialog)的實(shí)例代碼
2.自定義Dialog布局文件
3.可以自己封裝一個(gè)類,繼承自Dialog或者直接使用Dialog類來(lái)實(shí)現(xiàn),為了方便以后重復(fù)使用,建議自己封裝一個(gè)Dialog類
第一步:
我們知道Android定義個(gè)控件或View的樣式都是通過(guò)定義其style來(lái)實(shí)現(xiàn)的,查看Android框架中的主題文件,在源碼中的路徑:/frameworks/base/core/res/res/values/themes.xml,我們可以看到,Android為Dialog定義了一個(gè)樣式,
<style name="Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowTitleStyle">@android:style/DialogWindowTitle</item>
<item name="android:windowBackground">@android:drawable/panel_background</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
</style>
我們可以看到,在Themes.xml中定義的Dialog的樣式,其中,定義了window的標(biāo)題樣式,window的背景圖,是否懸浮等等。
那么,我們要?jiǎng)?chuàng)建具有自定義樣式的Dialog就可以創(chuàng)建一個(gè)styles.xml,在其中定義我們自己的Dialog樣式,讓其繼承自Theme.Dialog樣式,并修改其中的某些屬性即可。
定義我們自己的Dialog樣式:
a.創(chuàng)建一個(gè)styles.xml文件,放在res/values 文件夾下(當(dāng)然了,這就不用說(shuō)了。。。啰嗦一下)
b.在styles.xml中定義Dialog樣式,代碼如下:
<style name="Theme_dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
</style>
上面代碼中,定義了一個(gè)樣式Theme_dialog,繼承自@android:style/Theme.Dialog,然后定義了Dialog所在Window的背景圖,此處使用的是透明顏色#00000000,然后定義了Dialog所在的Window隱藏標(biāo)題(系統(tǒng)定義Dialog樣式是帶有標(biāo)題的,在此設(shè)置此屬性為true可隱藏標(biāo)題),自定義Dialog樣式到這就完成了。
第二步:
定義Dialog的布局:
創(chuàng)建一個(gè)布局文件layout_dialog.xml,代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/pp_bg_dialog"
android:gravity="center">
<ProgressBar android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/progressbar_normal"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
style="@style/text_white_small" android:layout_marginTop="5dp"
android:text="正在刪除..." android:id="@+id/message"/>
</LinearLayout>
這里使用了一個(gè)垂直方向的線性布局,并且設(shè)置所有子元素居中,子元素為已個(gè)進(jìn)度條ProgressBar和一個(gè)TextView。
此處,ProgressBar采用自定義樣式,使用系統(tǒng)默認(rèn)的ProgressBar可達(dá)到同樣的效果(大同小異)。LinearLayout的背景
android:background="@drawable/pp_bg_dialog"(即上面效果圖中居中顯示的黑色透明背景框)是一個(gè)自定義的圖片資源Shape:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="10dp"/>
<solid android:color="#55000000"/>
</shape>
代碼中定義了一個(gè)矩形,并設(shè)置了圓角和顏色。到此,Dialog的布局就完成了。
第三步:
自定義CustomDialog類,繼承自Dialog,代碼如下:
public class CustomDialog extends Dialog { 2
private static int default_width = 160; //默認(rèn)寬度
private static int default_height = 120;//默認(rèn)高度
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);12
//set content
setContentView(layout);
//set window params
Window window = getWindow();
WindowManager.LayoutParams params = window.getAttributes();
//set width,height by density and gravity
float density = getDensity(context);
params.width = (int) (width*density);
params.height = (int) (height*density);
params.gravity = Gravity.CENTER;
window.setAttributes(params);
}
private float getDensity(Context context) {
Resources resources = context.getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
return dm.density;
}
}
在構(gòu)造方法中設(shè)置了Dialog的contentView,并且設(shè)置了Window的寬度、高度和居中顯示。
CustomDialog使用方法如下:
public class CustomDialogDemoActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
CustomDialog dialog1 = new CustomDialog(this, R.layout.common_dialog, R.style.Theme_dialog);//Dialog使用默認(rèn)大小(160)
CustomDialog dialog2 = new CustomDialog(this, 180, 180, R.layout.common_dialog, R.style.Theme_dialog);
dialog2.show();//顯示Dialog
//如果要修改Dialog中的某個(gè)View,比如把"正在刪除..."改為"加載中..."
TextView mMessage = (TextView) dialog2.findViewById(R.id.message);
mMessage.setText("加載中...");
}
}
大體過(guò)程就是這樣,根據(jù)以上大家可以自由發(fā)揮吧,希望都設(shè)計(jì)出自己滿意的dialog。
- Android自定義對(duì)話框Dialog的簡(jiǎn)單實(shí)現(xiàn)
- Android實(shí)現(xiàn)底部對(duì)話框BottomDialog彈出實(shí)例代碼
- 詳解Android 全局彈出對(duì)話框SYSTEM_ALERT_WINDOW權(quán)限
- Android實(shí)現(xiàn)點(diǎn)擊AlertDialog上按鈕時(shí)不關(guān)閉對(duì)話框的方法
- 實(shí)例詳解Android自定義ProgressDialog進(jìn)度條對(duì)話框的實(shí)現(xiàn)
- Android 之BottomsheetDialogFragment仿抖音評(píng)論底部彈出對(duì)話框效果(實(shí)例代碼)
- Android實(shí)現(xiàn)退出界面彈出提示對(duì)話框
- Android中AlertDialog各種對(duì)話框的用法實(shí)例詳解
- Android仿QQ消息提示實(shí)現(xiàn)彈出式對(duì)話框
- Android對(duì)話框使用方法詳解
相關(guān)文章
Android Zygote啟動(dòng)構(gòu)造流程及進(jìn)程創(chuàng)建詳解
這篇文章主要為大家介紹了Android Zygote啟動(dòng)構(gòu)造流程及進(jìn)程創(chuàng)建詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07Android 調(diào)用系統(tǒng)聯(lián)系人界面(添加聯(lián)系人,添加已有聯(lián)系人,編輯和修改)
這篇文章主要介紹了Android 調(diào)用系統(tǒng)聯(lián)系人界面(添加聯(lián)系人,添加已有聯(lián)系人,編輯和修改),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03詳解Android WebView的input上傳照片的兼容問(wèn)題
本篇文章主要介紹了詳解Android WebView的input上傳照片的兼容問(wèn)題,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-08-08Android?中?FrameLayout?布局及屬性的使用詳解
這篇文章主要介紹了Android?中?FrameLayout?布局及屬性的使用,FrameLayout?在實(shí)現(xiàn)簡(jiǎn)單布局時(shí)非常方便,特別適用于疊加式布局,如顯示疊加的圖層或浮動(dòng)按鈕等,需要的朋友可以參考下2024-03-03Android動(dòng)態(tài)權(quán)限申請(qǐng)實(shí)現(xiàn)步驟分解
對(duì)于一些危險(xiǎn)權(quán)限在AndroidManifest清單文件中申請(qǐng)之后,還需要得到用戶的許可并打開(kāi),才算是真正的開(kāi)啟了這個(gè)權(quán)限。所以可以使用動(dòng)態(tài)申請(qǐng)權(quán)限,對(duì)于某個(gè)功能,如果需要開(kāi)啟某個(gè)權(quán)限,在用戶使用它之前,彈窗提示用戶是否要開(kāi)啟這個(gè)權(quán)限2023-04-04Android實(shí)現(xiàn)跟隨手指拖動(dòng)并自動(dòng)貼邊的View樣式(實(shí)例demo)
本文通過(guò)實(shí)例代碼給大家介紹了android實(shí)現(xiàn)跟隨手指拖動(dòng)并自動(dòng)貼邊的View樣式,效果非常棒,具有參考借鑒價(jià)值,需要的朋友參考下吧2017-01-01Android 開(kāi)發(fā)中l(wèi)ayout下的子文件夾
這篇文章主要介紹了android 開(kāi)發(fā)中l(wèi)ayout下的子文件夾,需要的朋友可以參考下2017-12-12Android實(shí)現(xiàn)使用流媒體播放遠(yuǎn)程mp3文件的方法
這篇文章主要介紹了Android實(shí)現(xiàn)使用流媒體播放遠(yuǎn)程mp3文件的方法,結(jié)合實(shí)例形式分析了Android遠(yuǎn)程播放音頻文件的相關(guān)步驟與實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-08-08