網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
本文實(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();
? ? ? ? ? ? }
? ? ? ? });
? ? }
原文鏈接:https://blog.csdn.net/weixin_42630638/article/details/109316022
相關(guān)推薦
- 2023-07-22 Redis使用教程之jedis客戶端sendCommand方法的byte[]入?yún)ⅲ乐够煜齭trin
- 2023-10-31 IP地址、網(wǎng)關(guān)、網(wǎng)絡(luò)/主機(jī)號(hào)、子網(wǎng)掩碼關(guān)系
- 2022-12-24 React?組件的狀態(tài)下移和內(nèi)容提升的操作方法_React
- 2022-04-16 WPF框架Prism中View?Injection用法介紹_基礎(chǔ)應(yīng)用
- 2022-03-17 VSCode如何遠(yuǎn)程連接Linux教程(vscode怎么連接ssh遠(yuǎn)程)
- 2024-01-06 Springboot 啟動(dòng)報(bào)錯(cuò) The bean ‘xxxx‘, defined in class
- 2022-04-09 SpringMVC 基礎(chǔ)配置文件(簡(jiǎn)潔版本)
- 2022-07-12 ERROR:ORA-12543: TNS:destination host unreachable
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支