Android自定義彈框Dialog效果
本文實例為大家分享了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(); //獲取對話框當(dāng)前的參數(shù)值 ? ? ? ? p.width = (int) (displayWidth * 0.8); //寬度設(shè)置為屏幕的0.5 ? ? ? ? //dialog.setCanceledOnTouchOutside(false);// 設(shè)置點擊屏幕Dialog不消失 ? ? ? ? dialog.getWindow().setAttributes(p); ?//設(shè)置生效 ? ? ? ? dialog.show(); ? ? ? ? //點擊關(guān)閉 ? ? ? ? delete_close_id.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? dialog.dismiss(); ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? //點擊確定刪除 ? ? ? ? delete_sure_id.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? dialog.dismiss(); ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? //點擊取消刪除 ? ? ? ? delete_cancle_id.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? dialog.dismiss(); ? ? ? ? ? ? } ? ? ? ? }); ? ? }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 判斷某個服務(wù)(service)是否運行
這篇文章主要介紹了 Android 判斷某個服務(wù)(service)是否運行的相關(guān)資料,需要的朋友可以參考下2017-06-06Android基于AccessibilityService制作的釘釘自動簽到程序代碼
這篇文章主要介紹了Android基于AccessibilityService制作的釘釘自動簽到程序代碼,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05Android點擊Button實現(xiàn)切換點擊圖片效果的示例
今天小編就為大家分享一篇關(guān)于Android點擊Button實現(xiàn)切換點擊圖片效果的示例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03Android實現(xiàn)文字和圖片混排(文字環(huán)繞圖片)效果
這篇文章主要介紹了Android實現(xiàn)文字和圖片混排的方法,實例分析了文字環(huán)繞圖片效果的具體功能顯示及頁面布局實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10Android Studio 下 Flutter 開發(fā)環(huán)境搭建過程
這篇文章主要介紹了Android Studio 下 Flutter 開發(fā)環(huán)境搭建/Flutter / Dart 插件安裝 | Flutter SDK 安裝 | 環(huán)境變量配置 | 開發(fā)環(huán)境檢查,本文圖文并茂給大家介紹的非常詳細,需要的朋友可以參考下2020-03-03Android實現(xiàn)簡單C/S聊天室應(yīng)用
這篇文章主要為大家詳細介紹了Android實現(xiàn)簡單C/S聊天室應(yīng)用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01Android開發(fā)之獲取網(wǎng)絡(luò)鏈接狀態(tài)
這篇文章主要介紹了Android獲取網(wǎng)絡(luò)鏈接狀態(tài)的方法,主要是通過ConnectivityManager類來完成的,需要的朋友可以參考下2014-08-08