自己写一个Dialog
CustonDialog还需要画一个布局layout_custom_dialog
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
>
<TextView
android:id="@+id/tv_custom_dialog_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="@color/colorBlack"
android:text="提示"
android:textStyle="bold"
android:layout_marginTop="15dp"
/>
<TextView
android:id="@+id/tv_custom_dialog_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="@color/colorBlack"
android:text="删除"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
/>
<View //大写啊哥
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/colorGreen"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:orientation="horizontal"
>
<TextView
android:id="@+id/tv_cancel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="取消"
android:textSize="20sp"
android:gravity="center"
/>
<View
android:layout_width="0.5dp"
android:layout_height="match_parent"
android:background="@color/colorBlack"
/>
<TextView
android:id="@+id/tv_confirm"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="确认"
android:textSize="20sp"
android:gravity="center"
/>
</LinearLayout>
<view
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/colorGreen"
/>
</LinearLayout>

CustonDialog
package com.example.test0508.widget;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import com.example.test0508.R;
public class CustonDialog extends Dialog implements View.OnClickListener {
private TextView mTvTitle,mTvMessage,mTvConfirm,mTvCancel;
private String title,message,cancel,confirm;
private IOnCancelListener cancelListener;
private IOnConfirmlListener confirmlListener;
public CustonDialog(@NonNull Context context) {
super(context);
}
public CustonDialog(@NonNull Context context, int themeresid) {
super(context, themeresid);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_custom_dialog);
mTvTitle = findViewById(R.id.tv_custom_dialog_title);
mTvMessage = findViewById(R.id.tv_custom_dialog_message);
mTvConfirm = findViewById(R.id.tv_confirm);
mTvCancel = findViewById(R.id.tv_cancel);
if (!TextUtils.isEmpty(title)){
mTvTitle.setText(title);
}
if (!TextUtils.isEmpty(message)){
mTvMessage.setText(message);
}
if (!TextUtils.isEmpty(cancel)){
mTvCancel.setText(cancel);
}
if (!TextUtils.isEmpty(confirm)){
mTvCancel.setText(confirm);
}
mTvCancel.setOnClickListener(this);
mTvConfirm.setOnClickListener(this);
}
public CustonDialog setTitle(String title) {
this.title = title;
return this;
}
public CustonDialog setMessage(String message) {
this.message = message;
return this;
}
public CustonDialog setCancel(String cancel,IOnCancelListener cancelListener) {
this.cancelListener = cancelListener;
this.cancel = cancel;
return this;
}
public CustonDialog setConfirm(String confirm,IOnConfirmlListener confirmlListener) {
this.confirmlListener = confirmlListener;
this.confirm = confirm;
return this;
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.tv_cancel:
if (cancelListener!=null){
cancelListener.onCancel(this);
}
dismiss();
break;
case R.id.tv_confirm:
if (confirmlListener!=null){
confirmlListener.onConfrim(this);
}
dismiss();
break;
}
}
public interface IOnCancelListener{
void onCancel(CustonDialog dialog);
}
public interface IOnConfirmlListener{
void onConfrim(CustonDialog dialog);
}
}
bg_custom_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<solid android:color="@color/colorGreen"/>
<corners android:radius="5dp" />
</shape>
DialogActivity以及它对应的页面布局文件
package com.example.test0508;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.example.test0508.widget.CustonDialog;
public class CustonDialogActivity extends AppCompatActivity {
private Button mBtnDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custon_dialog);
mBtnDialog = findViewById(R.id.btn_custom_dialog);
mBtnDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CustonDialog custonDialog = new CustonDialog(CustonDialogActivity.this);
custonDialog.setTitle("这是提示").setMessage("确认删除?")
.setCancel("要取消", new CustonDialog.IOnCancelListener() {
@Override
public void onCancel(CustonDialog dialog) {
}
}).setConfirm("还是确定", new CustonDialog.IOnConfirmlListener() {
@Override
public void onConfrim(CustonDialog dialog) {
}
}).show();
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/btn_custom_dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义Dialog"
android:textAllCaps="false"
/>
</LinearLayout>
本文介绍了如何在Android中创建自定义Dialog,包括绘制布局layout_custom_dialog,实现CustonDialog类,以及设置背景bg_custom_dialog.xml。同时展示了DialogActivity及其对应的页面布局文件使用方法。

1121

被折叠的 条评论
为什么被折叠?



