網站首頁 編程語言 正文
本文實例為大家分享了Android自定義彈框Dialog效果的具體代碼,供大家參考,具體內容如下
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="刪除設備" ? ? ? ? ? ? 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="設備 ? 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.設置背景邊框,在drawable創建dialog_white_back.xml
顏色以及圓角 根據自己需求修改
<?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創建round_gray.xml
顏色以及圓角 根據自己需求修改
<?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(); //獲取對話框當前的參數值
? ? ? ? p.width = (int) (displayWidth * 0.8); //寬度設置為屏幕的0.5
? ? ? ? //dialog.setCanceledOnTouchOutside(false);// 設置點擊屏幕Dialog不消失
? ? ? ? dialog.getWindow().setAttributes(p); ?//設置生效
? ? ? ? dialog.show();
? ? ? ? //點擊關閉
? ? ? ? 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();
? ? ? ? ? ? }
? ? ? ? });
? ? }
原文鏈接:https://blog.csdn.net/weixin_42630638/article/details/109316022
相關推薦
- 2022-06-25 Qt一個進程運行另一個進程的實現方法_C 語言
- 2023-12-06 Warn: Could not find @TableId
- 2023-04-02 使用Pytorch如何完成多分類問題_python
- 2022-05-25 如何把自己寫的jar包打進本地maven倉庫呢(也是springboot項目怎么打成SDK)
- 2022-12-11 Go?模塊在下游服務抖動恢復后CPU占用無法恢復原因_Golang
- 2022-11-01 Android?Activity?Results?API代替onActivityResult處理頁面
- 2022-08-27 Oracle縮表空間的完整解決實例_oracle
- 2022-04-21 C#實現chart控件動態曲線繪制_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支