詳解Android中Dialog的使用
在Android中經(jīng)常要使用Dialog來實(shí)現(xiàn)一些提示以及一些特殊的效果,而且樣式也不一樣,每次都得查一大堆資料,還不一定能解決,這里總結(jié)一些常用的Dialog的實(shí)踐。
普通的Dialog
//普通的AlertDialog對(duì)話框 findViewById(R.id.btn_common).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("普通的對(duì)話框的標(biāo)題"); builder.setMessage("這是一個(gè)普通的對(duì)話框的內(nèi)容"); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { toast("取消"); } }); builder.setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { toast("確定"); } }); AlertDialog dialog = builder.create(); dialog.show(); } });
這里使用AlertDialog來顯示一個(gè)系統(tǒng)的提示對(duì)話框,效果如下:
修改普通對(duì)話框的位置、大小、透明度
主要是在普通的dialog.show() 下面加上如下代碼
//放在show()之后,不然有些屬性是沒有效果的,比如height和width Window dialogWindow = dialog.getWindow(); WindowManager m = getWindowManager(); Display d = m.getDefaultDisplay(); // 獲取屏幕寬、高用 WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 獲取對(duì)話框當(dāng)前的參數(shù)值 //設(shè)置高度和寬度 p.height = (int) (d.getHeight() * 0.4); // 高度設(shè)置為屏幕的0.6 p.width = (int) (d.getWidth() * 0.6); // 寬度設(shè)置為屏幕的0.65 //設(shè)置位置 p.gravity = Gravity.BOTTOM; //設(shè)置透明度 p.alpha = 0.5f; dialogWindow.setAttributes(p);
在這里,設(shè)置dialog的高為屏幕的高度的4/10,寬為屏幕寬帶的6/10,同事位置為底部,透明度為半透明。當(dāng)然還有很多其他屬性,這里暫不介紹,你們可以自己試一試。效果如下:
使用普通的dialog來添加自定義布局
我們需自定義一個(gè)布局,如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="100dp" android:layout_height="100dp" android:background="#00ff00"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:textColor="#ff0000" android:text="你好"/> </LinearLayout>
我們這里新建了一個(gè)布局設(shè)置高度和寬度為100dp,線性布局里面包裹了一個(gè)TextView,布局很簡(jiǎn)單,當(dāng)然也可以自定義一個(gè)復(fù)雜的布局,這里就不介紹了。來看看Java代碼的實(shí)現(xiàn)。
// 使用普通的dialog來添加自定義布局 findViewById(R.id.btn_custom2).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setView(R.layout.dialog_custom1); AlertDialog dialog = builder.create(); dialog.show(); } });
我們直接把我們的布局通過builder設(shè)置進(jìn)去,看看效果:
這里的Dialog非常丑,這是與AlertDialog的默認(rèn)主題有關(guān),下面我們通過自定義主題來改變對(duì)話框的樣式來使對(duì)話框變得漂亮。
在values/styles.xml自定義樣式繼承Android:Theme.Dialog來實(shí)現(xiàn)自己的樣式
<style name="MyCommonDialog" parent="android:Theme.Dialog"> <!-- 背景顏色及透明程度 --> <item name="android:windowBackground">@android:color/transparent</item> <!-- 是否半透明 --> <item name="android:windowIsTranslucent">false</item> <!-- 是否沒有標(biāo)題 --> <item name="android:windowNoTitle">true</item> <!-- 是否浮現(xiàn)在activity之上 --> <item name="android:windowIsFloating">true</item> <!-- 是否背景模糊 --> <item name="android:backgroundDimEnabled">false</item> <!-- 設(shè)置背景模糊的透明度--> <item name="android:backgroundDimAmount">0.5</item> </style>
這里樣式的屬性都有注釋,沒種樣式不是必須的,你可以自己試著改變一些值來查看效果以便達(dá)到自己的最佳效果。
在創(chuàng)建dialog的時(shí)候?qū)邮絺鬟^去
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this,R.style.MyCommonDialog);
現(xiàn)在的效果如下:
可以看的我們的布局的高度和寬帶還是沒效果,我們知道子空間的布局一般由布局來測(cè)量的于是我想到給這個(gè)布局的最外層套一個(gè)布局,看能不能達(dá)到我們的效果。
修改dialog_custom1.xml布局如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="100dp" android:layout_height="100dp" android:layout_centerInParent="true" android:background="#00ff00"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="你好" android:textColor="#ff0000"/> </LinearLayout> </RelativeLayout>
再次運(yùn)行如下:
達(dá)到我們想要的效果了,這樣你就可以引入樣式和自定義布局實(shí)現(xiàn)各種對(duì)話框的效果了。
繼承Dialog來實(shí)現(xiàn)Dialog
通過繼承Dialog來實(shí)現(xiàn)自定義的Dialog,這樣我們就可以在任何地方直接new我們的Dialog就可以實(shí)現(xiàn)特定的對(duì)話框了。
1.在values/styles.xml新建一個(gè)樣式MyDialog
<style name="MyDialog" parent="android:Theme.Dialog"> <!-- 背景顏色及透明程度 --> <item name="android:windowBackground">@android:color/transparent</item> <!-- 是否半透明 --> <item name="android:windowIsTranslucent">false</item> <!-- 是否沒有標(biāo)題 --> <item name="android:windowNoTitle">true</item> <!-- 是否浮現(xiàn)在activity之上 --> <item name="android:windowIsFloating">true</item> <!-- 是否背景模糊 --> <item name="android:backgroundDimEnabled">false</item> <!-- 設(shè)置背景模糊的透明度--> <item name="android:backgroundDimAmount">0.5</item> </style>
2.新建一個(gè)MyDialog繼承Dialog
public class MyDialog extends Dialog { //在構(gòu)造方法里預(yù)加載我們的樣式,這樣就不用每次創(chuàng)建都指定樣式了 public MyDialog(Context context) { this(context, R.style.MyDialog); } public MyDialog(Context context, int themeResId) { super(context, themeResId); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 預(yù)先設(shè)置Dialog的一些屬性 Window dialogWindow = getWindow(); WindowManager.LayoutParams p = dialogWindow.getAttributes(); p.x = 0; p.y = 100; p.gravity = Gravity.LEFT | Gravity.TOP; dialogWindow.setAttributes(p); } }
/*
* lp.x與lp.y表示相對(duì)于原始位置的偏移.
* 當(dāng)參數(shù)值包含Gravity.LEFT時(shí),對(duì)話框出現(xiàn)在左邊,所以lp.x就表示相對(duì)左邊的偏移,負(fù)值忽略.
* 當(dāng)參數(shù)值包含Gravity.RIGHT時(shí),對(duì)話框出現(xiàn)在右邊,所以lp.x就表示相對(duì)右邊的偏移,負(fù)值忽略.
* 當(dāng)參數(shù)值包含Gravity.TOP時(shí),對(duì)話框出現(xiàn)在上邊,所以lp.y就表示相對(duì)上邊的偏移,負(fù)值忽略.
* 當(dāng)參數(shù)值包含Gravity.BOTTOM時(shí),對(duì)話框出現(xiàn)在下邊,所以lp.y就表示相對(duì)下邊的偏移,負(fù)值忽略.
* 當(dāng)參數(shù)值包含Gravity.CENTER_HORIZONTAL時(shí),對(duì)話框水平居中,所以lp.x就表示在水平居中的位置移動(dòng)
* lp.x像素,正值向右移動(dòng),負(fù)值向左移動(dòng).
* 當(dāng)參數(shù)值包含Gravity.CENTER_VERTICAL時(shí),對(duì)話框垂直居中,所以lp.y就表示在垂直居中的位置移動(dòng)lp.y像
* 素,正值向右移動(dòng),負(fù)值向左移動(dòng).
* gravity的默認(rèn)值為Gravity.CENTER,即Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL
*/
這里對(duì)window的一些參數(shù)進(jìn)行了解釋,我把對(duì)話框設(shè)置的離左上角離頂部100px的位置。
3.使用MyDialog
自定義布局dialog_custom2.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true" android:background="#ffffff" android:orientation="vertical" android:padding="10dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="2dp" android:background="#00ff00" android:gravity="center" android:padding="10dp" android:text="你好" android:textColor="#000000"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="2dp" android:background="#00ff00" android:gravity="center" android:padding="10dp" android:text="你好" android:textColor="#000000"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="2dp" android:background="#00ff00" android:gravity="center" android:padding="10dp" android:text="你好" android:textColor="#000000"/> </LinearLayout> </RelativeLayout>
Java代碼
//繼承Dialog來實(shí)現(xiàn)Dialog findViewById(R.id.btn_custom3).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { MyDialog dialog = new MyDialog(MainActivity.this); dialog.setContentView(R.layout.dialog_custom2); dialog.show(); } });
4.查看效果:
給Dialog設(shè)置動(dòng)畫
1.新建動(dòng)畫文件
進(jìn)入動(dòng)畫dialog_enter.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="200" android:fillAfter="true" android:fromYDelta="100%p" android:toYDelta="0%"/> </set>
退出動(dòng)畫dialog_exit.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="200" android:fillAfter="true" android:fromYDelta="0%" android:toYDelta="100%p"/> </set>
2.在values/styles.xml中新建樣式
<style name="MyAnimDialog" parent="android:Theme.Dialog"> <!-- 背景顏色及透明程度 --> <item name="android:windowBackground">@android:color/transparent</item> <!-- 是否半透明 --> <item name="android:windowIsTranslucent">false</item> <!-- 是否沒有標(biāo)題 --> <item name="android:windowNoTitle">true</item> <!-- 是否浮現(xiàn)在activity之上 --> <item name="android:windowIsFloating">true</item> <!-- 是否背景模糊 --> <item name="android:backgroundDimEnabled">true</item> <!-- 設(shè)置背景模糊的透明度--> <item name="android:backgroundDimAmount">0.5</item> <!-- 動(dòng)畫 --> <item name="android:windowAnimationStyle">@style/dialog_animation</item> </style> <!-- 對(duì)話框顯示和退出動(dòng)畫 --> <style name="dialog_animation"> <item name="android:windowEnterAnimation">@anim/dialog_enter</item> <item name="android:windowExitAnimation">@anim/dialog_exit</item> </style>
主要是給android:windowAnimationStyle指定我們新建的動(dòng)畫即可,引用和前面一樣,這里就給出了,
3.查看效果
繼承Dialog來實(shí)現(xiàn)底部彈出Dialog
自定義MyBottomDialog
public class MyBottomDialog extends Dialog { public MyBottomDialog(Context context) { this(context, R.style.MyAnimDialog); } public MyBottomDialog(Context context, int themeResId) { super(context, themeResId); //加載布局并給布局的控件設(shè)置點(diǎn)擊事件 View contentView = getLayoutInflater().inflate(R.layout.dialog_custom3, null); contentView.findViewById(R.id.tv_1).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getContext(), "你好", Toast.LENGTH_SHORT).show(); } }); super.setContentView(contentView); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 預(yù)先設(shè)置Dialog的一些屬性 Window dialogWindow = getWindow(); WindowManager.LayoutParams p = dialogWindow.getAttributes(); WindowManager m = getWindow().getWindowManager(); Display d = m.getDefaultDisplay(); getWindow().setAttributes(p); p.height = (int) (d.getHeight() * 0.6); p.width = d.getWidth(); p.gravity = Gravity.LEFT | Gravity.BOTTOM; dialogWindow.setAttributes(p); } }
在onCreate方法里指定的Dialog的高度和寬度
布局dialog_custom3.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true" android:background="#ffffff" android:orientation="vertical" android:padding="10dp"> <TextView android:id="@+id/tv_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="2dp" android:background="#00ff00" android:gravity="center" android:padding="10dp" android:text="你好" android:textColor="#000000"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="2dp" android:background="#00ff00" android:gravity="center" android:padding="10dp" android:text="你好" android:textColor="#000000"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="2dp" android:background="#00ff00" android:gravity="center" android:padding="10dp" android:text="你好" android:textColor="#000000"/> </LinearLayout> </RelativeLayout>
使用是方法是一樣的
//繼承Dialog來實(shí)現(xiàn)底部彈出Dialog findViewById(R.id.btn_custom5).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { MyBottomDialog dialog = new MyBottomDialog(MainActivity.this); dialog.show(); } });
這里就不用設(shè)置布局了,因?yàn)槲覀冊(cè)贛yBottomDialog的構(gòu)造方法里已經(jīng)預(yù)加載了布局并設(shè)置了點(diǎn)擊事件
查看效果:
自定義仿Meun的彈出Dialog
MyMenuDialog的代碼
public class MyMenuDialog extends Dialog { public MyMenuDialog(Context context) { this(context, R.style.MyDialog); } public MyMenuDialog(Context context, int themeResId) { super(context, themeResId); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 預(yù)先設(shè)置Dialog的一些屬性 Window dialogWindow = getWindow(); WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 獲取對(duì)話框當(dāng)前的參數(shù)值 //獲取屏幕的高度和寬帶 WindowManager m = getWindow().getWindowManager(); Display d = m.getDefaultDisplay(); DisplayMetrics outMetrics = new DisplayMetrics(); d.getMetrics(outMetrics); //設(shè)置WindowManager.LayoutParams // p.height = (int) (outMetrics.heightPixels * 0.6); // p.width = (int) (outMetrics.widthPixels * 0.4); //根據(jù)s隨意來的高度來設(shè)置x軸偏移量 p.x = (int) (15 * outMetrics.density); //根據(jù)Title的高度來設(shè)置y軸偏移量 p.y = (int) (45 * outMetrics.density); p.gravity = Gravity.RIGHT | Gravity.TOP; dialogWindow.setAttributes(p); } }
使用就不介紹了,這里主要是利用WindowManager.LayoutParams的x、y、gravity來實(shí)現(xiàn)的,當(dāng)然可以自定義Dialog的彈出動(dòng)畫就可以實(shí)現(xiàn)一個(gè)菜單對(duì)話框了。
效果如下:
基本上Dialog的實(shí)現(xiàn)了這些效果應(yīng)該能滿足大部分項(xiàng)目的需求,至于以下復(fù)雜的,想帶有ListView、GridView的Dialog等等都可以通過自定義Dialog來繼承Dialog來實(shí)現(xiàn),都是依葫蘆畫瓢就可以了,以后遇到什么再來補(bǔ)充。
- android控件封裝 自己封裝的dialog控件
- Android中自定義對(duì)話框(Dialog)的實(shí)例代碼
- android dialog自定義實(shí)例詳解
- Android中Dialog去黑邊的方法
- Android開發(fā)筆記之:Dialog的使用詳解
- Android ProgressBar進(jìn)度條和ProgressDialog進(jìn)度框的展示DEMO
- Android 去掉自定義dialog的白色邊框的簡(jiǎn)單方法
- Android修改源碼解決Alertdialog觸摸對(duì)話框邊緣消失的問題
- Android Dialog 設(shè)置字體大小的具體方法
- Android入門之AlertDialog用法實(shí)例分析
- Android自定義ProgressDialog進(jìn)度等待框
- 實(shí)例詳解Android自定義ProgressDialog進(jìn)度條對(duì)話框的實(shí)現(xiàn)
- 淺析Android中強(qiáng)大的Dialog
相關(guān)文章
Android自定義ImageView實(shí)現(xiàn)圓角功能
這篇文章主要為大家詳細(xì)介紹了Android自定義ImageView實(shí)現(xiàn)圓角功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12Android項(xiàng)目實(shí)戰(zhàn)之ListView懸浮頭部展現(xiàn)效果實(shí)現(xiàn)
這篇文章主要給大家介紹了Android項(xiàng)目實(shí)戰(zhàn)之ListView懸浮頭部展現(xiàn)效果實(shí)現(xiàn)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01Kotlin Select協(xié)程多路復(fù)用的實(shí)現(xiàn)詳解
select是Kotlin 1.6中的特性,即選擇最快的結(jié)果。select與async、Channel結(jié)合使用,可以大大提高程序的響應(yīng)速度,還可以提高程序的靈活性、擴(kuò)展性2022-09-09android kotlin集成WorkManager實(shí)現(xiàn)定時(shí)獲取數(shù)據(jù)的步驟
在Android中使用Kotlin集成WorkManager來實(shí)現(xiàn)定時(shí)獲取數(shù)據(jù)是一個(gè)很常見的需求,下面給大家分享android kotlin集成WorkManager實(shí)現(xiàn)定時(shí)獲取數(shù)據(jù)的步驟,感興趣的朋友跟隨小編一起看看吧2024-08-08Android點(diǎn)擊Button實(shí)現(xiàn)切換點(diǎn)擊圖片效果的示例
今天小編就為大家分享一篇關(guān)于Android點(diǎn)擊Button實(shí)現(xiàn)切換點(diǎn)擊圖片效果的示例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03Android?NotificationListenerService通知監(jiān)聽服務(wù)使用
這篇文章主要為大家介紹了Android?NotificationListenerService通知監(jiān)聽服務(wù)使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Android自定義ProgressDialog進(jìn)度等待框
這篇文章主要介紹了Android自定義ProgressDialog進(jìn)度等待框,通過本文大家可以嘗試?yán)肁ndroid自定義ProgressDialog,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-01-01Android ListView里控件添加監(jiān)聽方法的實(shí)例詳解
這篇文章主要介紹了Android ListView里控件添加監(jiān)聽方法的實(shí)例詳解的相關(guān)資料,這里提供實(shí)例幫助大家學(xué)習(xí)理解這部分內(nèi)容,需要的朋友可以參考下2017-08-08Handler實(shí)現(xiàn)倒計(jì)時(shí)功能
這篇文章主要為大家詳細(xì)介紹了Handler實(shí)現(xiàn)倒計(jì)時(shí)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04