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 開(kāi)發(fā)手機(jī)(三星)拍照應(yīng)用照片旋轉(zhuǎn)問(wèn)題解決辦法
這篇文章主要介紹了Android 開(kāi)發(fā)手機(jī)(三星)拍照應(yīng)用照片旋轉(zhuǎn)問(wèn)題解決辦法的相關(guān)資料,需要的朋友可以參考下2017-04-04
Android監(jiān)聽(tīng)鍵盤狀態(tài)獲取鍵盤高度的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于Android監(jiān)聽(tīng)鍵盤狀態(tài)獲取鍵盤高度的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Android添加ButterKnife時(shí)報(bào)錯(cuò)Error:(2, 0) Cannot add extension wit
今天小編就為大家分享一篇關(guān)于Android添加ButterKnife時(shí)報(bào)錯(cuò)Error:(2, 0) Cannot add extension with name 'android'的解決辦法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
Android中操作SQLite數(shù)據(jù)庫(kù)快速入門教程
這篇文章主要介紹了Android中操作SQLite數(shù)據(jù)庫(kù)快速入門教程,本文講解了數(shù)據(jù)庫(kù)基礎(chǔ)概念、Android平臺(tái)下數(shù)據(jù)庫(kù)相關(guān)類、創(chuàng)建數(shù)據(jù)庫(kù)、向表格中添加數(shù)據(jù)、從表格中查詢記錄等內(nèi)容,需要的朋友可以參考下2015-03-03
Android開(kāi)發(fā)教程之如何屏蔽View的重復(fù)點(diǎn)擊
這篇文章主要給大家介紹了關(guān)于Android開(kāi)發(fā)教程之如何屏蔽View的重復(fù)點(diǎn)擊的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09
Android Studio 設(shè)置代碼提示和代碼自動(dòng)補(bǔ)全快捷鍵方式
這篇文章主要介紹了Android Studio 設(shè)置代碼提示和代碼自動(dòng)補(bǔ)全快捷鍵方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
淺析Android App的相對(duì)布局RelativeLayout
這篇文章主要介紹了Android App的相對(duì)布局RelativeLayout,文中舉了一個(gè)登錄界面的XML布局例子,非常直觀,需要的朋友可以參考下2016-04-04
Android使用DrawerLayout實(shí)現(xiàn)側(cè)滑菜單效果
這篇文章主要為大家詳細(xì)介紹了Android使用DrawerLayout實(shí)現(xiàn)側(cè)滑菜單效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08

