Android自定義彈框Dialog效果
本文實(shí)例為大家分享了Android自定義彈框Dialog效果的具體代碼,供大家參考,具體內(nèi)容如下
1.dialog_delete.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"> ? ? <RelativeLayout ? ? ? ? android:layout_width="236dp" ? ? ? ? android:layout_height="184dp" ? ? ? ? android:layout_centerHorizontal="true" ? ? ? ? android:layout_centerVertical="true" ? ? ? ? android:background="@drawable/dialog_white_back"> ? ? ? ? <TextView ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:text="刪除設(shè)備" ? ? ? ? ? ? android:textColor="#333333" ? ? ? ? ? ? android:textSize="15sp" ? ? ? ? ? ? android:layout_centerHorizontal="true" ? ? ? ? ? ? android:layout_marginTop="13dp"></TextView> ? ? ? ? <ImageView ? ? ? ? ? ? android:id="@+id/delete_close_id" ? ? ? ? ? ? android:layout_width="10dp" ? ? ? ? ? ? android:layout_height="10dp" ? ? ? ? ? ? android:background="@mipmap/login_close_back" ? ? ? ? ? ? android:layout_alignParentRight="true" ? ? ? ? ? ? android:layout_marginTop="16dp" ? ? ? ? ? ? android:layout_marginRight="13dp"></ImageView> ? ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/delete_device_id" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:text="設(shè)備 ? ID:123456789" ? ? ? ? ? ? android:textColor="#333333" ? ? ? ? ? ? android:textSize="16sp" ? ? ? ? ? ? android:layout_marginTop="75dp" ? ? ? ? ? ? android:layout_centerHorizontal="true"></TextView> ? ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/delete_cancle_id" ? ? ? ? ? ? android:layout_width="77dp" ? ? ? ? ? ? android:layout_height="26dp" ? ? ? ? ? ? android:background="@drawable/round_gray" ? ? ? ? ? ? android:layout_alignParentBottom="true" ? ? ? ? ? ? android:layout_marginBottom="18dp" ? ? ? ? ? ? android:layout_marginLeft="31dp" ? ? ? ? ? ? android:text="取消" ? ? ? ? ? ? android:textSize="11sp" ? ? ? ? ? ? android:textColor="#333333" ? ? ? ? ? ? android:gravity="center"></TextView> ? ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/delete_sure_id" ? ? ? ? ? ? android:layout_width="77dp" ? ? ? ? ? ? android:layout_height="26dp" ? ? ? ? ? ? android:background="@drawable/round_blue" ? ? ? ? ? ? android:layout_alignParentBottom="true" ? ? ? ? ? ? android:layout_marginBottom="18dp" ? ? ? ? ? ? android:layout_alignParentRight="true" ? ? ? ? ? ? android:layout_marginRight="32dp" ? ? ? ? ? ? android:text="確定" ? ? ? ? ? ? android:textSize="11sp" ? ? ? ? ? ? android:textColor="#FEFDFD" ? ? ? ? ? ? android:gravity="center"></TextView> ? ? </RelativeLayout> </RelativeLayout>
2.設(shè)置背景邊框,在drawable創(chuàng)建dialog_white_back.xml
顏色以及圓角 根據(jù)自己需求修改
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:shape="rectangle"> ? ? ? <solid android:color ="#ffffff"/> ? ? <corners ? ? ? ? android:radius="8dp"/> </shape>
3.按鈕的背景邊框,在drawable創(chuàng)建round_gray.xml
顏色以及圓角 根據(jù)自己需求修改
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:shape="rectangle"> ? ? ? <solid android:color = "#DCDCDC" /> ? ? <corners ? ? ? ? android:radius="360dp"/> </shape>
4.在Style文件中添加
<!--Dialog 樣式 --> ? ? <style name="BottomDialog" parent="AlertDialog.AppCompat"> ? ? ? ? <item name="android:windowIsFloating">true</item> ? ? ? ? <item name="android:windowFrame">@null</item> ? ? ? ? <item name="android:windowNoTitle">true</item> ? ? ? ? <item name="android:windowBackground">@android:color/transparent</item> ? ? ? ? <item name="android:backgroundDimEnabled">true</item> ? ? ? ? <item name="android:windowContentOverlay">@null</item> ? ? ? ? <item name="android:fullBright">@color/clear</item> ? ? ? ? <item name="android:fullDark">@color/clear</item> ? ? ? ? <item name="android:topBright">@color/clear</item> ? ? ? ? <item name="android:topDark">@color/clear</item> ? ? ? ? <item name="android:borderlessButtonStyle">@color/clear</item> </style>
5.Java代碼部分
/** ? ? ?* 刪除AlertDialog ? ? ?*/ ? ? public void DeleteDialog() { ? ? ? ? //布局、id ? ? ? ? View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_delete, null); ? ? ? ? ImageView delete_close_id = view.findViewById(R.id.delete_close_id); ? ? ? ? TextView delete_device_id = view.findViewById(R.id.delete_device_id); ? ? ? ? TextView delete_cancle_id = view.findViewById(R.id.delete_cancle_id); ? ? ? ? TextView delete_sure_id = view.findViewById(R.id.delete_sure_id); ? ? ? ? //顯示樣式 ? ? ? ? final Dialog dialog = new Dialog(getActivity(), R.style.BottomDialog); ? ? ? ? dialog.setContentView(view); ? ? ? ? dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent); ? ? ? ? ? DisplayMetrics dm = getResources().getDisplayMetrics(); ? ? ? ? int displayWidth = dm.widthPixels; ? ? ? ? int displayHeight = dm.heightPixels; ? ? ? ? android.view.WindowManager.LayoutParams p = dialog.getWindow().getAttributes(); //獲取對(duì)話框當(dāng)前的參數(shù)值 ? ? ? ? p.width = (int) (displayWidth * 0.8); //寬度設(shè)置為屏幕的0.5 ? ? ? ? //dialog.setCanceledOnTouchOutside(false);// 設(shè)置點(diǎn)擊屏幕Dialog不消失 ? ? ? ? dialog.getWindow().setAttributes(p); ?//設(shè)置生效 ? ? ? ? dialog.show(); ? ? ? ? //點(diǎn)擊關(guān)閉 ? ? ? ? delete_close_id.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? dialog.dismiss(); ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? //點(diǎn)擊確定刪除 ? ? ? ? delete_sure_id.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? dialog.dismiss(); ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? //點(diǎn)擊取消刪除 ? ? ? ? delete_cancle_id.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? dialog.dismiss(); ? ? ? ? ? ? } ? ? ? ? }); ? ? }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android基礎(chǔ)開(kāi)發(fā)之手勢(shì)識(shí)別
這篇文章主要為大家詳細(xì)介紹了Android基礎(chǔ)開(kāi)發(fā)之手勢(shì)識(shí)別的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-06-06Android 判斷某個(gè)服務(wù)(service)是否運(yùn)行
這篇文章主要介紹了 Android 判斷某個(gè)服務(wù)(service)是否運(yùn)行的相關(guān)資料,需要的朋友可以參考下2017-06-06Android基于AccessibilityService制作的釘釘自動(dòng)簽到程序代碼
這篇文章主要介紹了Android基于AccessibilityService制作的釘釘自動(dòng)簽到程序代碼,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05Android點(diǎn)擊Button實(shí)現(xiàn)切換點(diǎn)擊圖片效果的示例
今天小編就為大家分享一篇關(guān)于Android點(diǎn)擊Button實(shí)現(xiàn)切換點(diǎn)擊圖片效果的示例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03Android實(shí)現(xiàn)文字和圖片混排(文字環(huán)繞圖片)效果
這篇文章主要介紹了Android實(shí)現(xiàn)文字和圖片混排的方法,實(shí)例分析了文字環(huán)繞圖片效果的具體功能顯示及頁(yè)面布局實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10Android Studio 下 Flutter 開(kāi)發(fā)環(huán)境搭建過(guò)程
這篇文章主要介紹了Android Studio 下 Flutter 開(kāi)發(fā)環(huán)境搭建/Flutter / Dart 插件安裝 | Flutter SDK 安裝 | 環(huán)境變量配置 | 開(kāi)發(fā)環(huán)境檢查,本文圖文并茂給大家介紹的非常詳細(xì),需要的朋友可以參考下2020-03-03Android編程開(kāi)發(fā)音樂(lè)播放器實(shí)例
這篇文章主要介紹了Android編程開(kāi)發(fā)音樂(lè)播放器,結(jié)合實(shí)例形式分析了Android音樂(lè)播放器開(kāi)發(fā)所涉及的SeekBar,ListView,廣播接收者(以代碼的形式注冊(cè)Receiver),系統(tǒng)服務(wù),MediaPlayer等技巧,需要的朋友可以參考下2016-01-01Android實(shí)現(xiàn)簡(jiǎn)單C/S聊天室應(yīng)用
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)單C/S聊天室應(yīng)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01Android開(kāi)發(fā)之獲取網(wǎng)絡(luò)鏈接狀態(tài)
這篇文章主要介紹了Android獲取網(wǎng)絡(luò)鏈接狀態(tài)的方法,主要是通過(guò)ConnectivityManager類(lèi)來(lái)完成的,需要的朋友可以參考下2014-08-08