網站首頁 編程語言 正文
1.實現效果
??
2.定義dialog.xml (res/layout/dialog.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" > <RelativeLayout android:layout_width="250sp" android:layout_height="270sp" android:layout_centerInParent="true"> <TextView android:id="@+id/dialog_title" android:layout_width="wrap_content" android:layout_height="35sp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="15sp" android:textColor="#333333" android:textSize="17sp" /> <TextView android:id="@+id/dialog_message" android:layout_width="wrap_content" android:layout_height="160sp" android:layout_centerInParent="true" android:layout_marginTop="65sp" android:textColor="#333333" android:textSize="17sp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginTop="30sp" android:layout_marginBottom="20sp" android:orientation="horizontal"> <LinearLayout android:id="@+id/dialog_confirm" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/confirm_button_style" android:gravity="center" android:text="確定" android:textColor="@color/white" android:textSize="18sp" /> </LinearLayout> <LinearLayout android:id="@+id/dialog_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="22sp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/cancel_button_style" android:gravity="center" android:text="取消" android:textColor="@color/teal_200" android:textSize="18sp" /> </LinearLayout> </LinearLayout> </RelativeLayout> </RelativeLayout>
3. 設置確定、取消按鈕的background
上文的dialog.xml中,確定和取消按鈕都是TextView,所以需要自定義按鈕的背景
confirm_button_style.xml? (所有的color需要自定義)
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="1000sp"/> <solid android:color="@color/teal_200"/> <stroke android:width="0.5sp" android:color="@color/colorAccent"/> <size android:width="105sp" android:height="40sp"/> </shape>
cancel_button_style.xml?
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="1000sp"/> <solid android:color="@color/white"/> <stroke android:width="0.5sp" android:color="@color/teal_200"/> <size android:width="105sp" android:height="40sp"/> </shape>
4. 自定義dialog的使用
final AlertDialog dialog = new AlertDialog.Builder(xxxClass.this).create(); dialog.setCancelable(false); //點擊對話框以外的位置,不消失 dialog.show(); Window window = dialog.getWindow(); window.setContentView(R.layout.dialog); //標題 TextView title = window.findViewById(R.id.dialog_title); title.setText("dialog_title"); //內容 TextView message = window.findViewById(R.id.dialog_message); message.setText("dialog_message "); //確定按鈕 LinearLayout confirm = window.findViewById(R.id.dialog_confirm); confirm.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //xxx } }); //取消按鈕 LinearLayout cancel = window.findViewById(R.id.dialog_cancel); cancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //xxx } });
原文鏈接:https://blog.csdn.net/java13245/article/details/122198519
相關推薦
- 2024-01-31 在 Nginx 配置中,root 和 alias 指令的區別是什么
- 2022-07-06 YOLOv5目標檢測之anchor設定_python
- 2022-06-12 C語言數學問題與簡單DP01背包問題詳解_C 語言
- 2022-05-06 利用python繪制笛卡爾直角坐標系_python
- 2023-07-15 oracle 序列/自增ID
- 2023-06-04 C#中+=是什么意思及+=的用法_C#教程
- 2022-03-22 C++抽象數據類型介紹_C 語言
- 2023-02-03 VB十七種可用一行代碼完成判斷的技巧代碼_vb
- 最近更新
-
- 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同步修改后的遠程分支