Android 自定义对话框Dialog
程序员文章站
2022-07-03 09:19:14
开发时候需要对整个APP风格统一,所以对话框这种都是需要统一样式的,这种就需要自定义对话框了,可以自定义设置左右按钮文字,标题、内容提示、标题下面还有图标效果图:代码使用:CommonDialog dialog = new CommonDialog(getActivity()); dialog.setMessage(message) .setTitle("提示") .setSingle(false)...
开发时候需要对整个APP风格统一,所以对话框这种都是需要统一样式的,这种就需要自定义对话框了,可以自定义设置左右按钮文字,标题、内容提示、标题下面还有图标
效果图:
代码使用:
CommonDialog dialog = new CommonDialog(getActivity());
dialog.setMessage(message)
.setTitle("提示")
.setSingle(false)
.setNegtive("取消")
.setPositive("确定").setOnClickBottomListener(new CommonDialog.OnClickBottomListener() {
@Override
public void onPositiveClick() {
dialog.dismiss();
//确定操作
}
@Override
public void onNegtiveClick() {
dialog.dismiss();
//取消操作
}
}).show();
具体实现代码
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.ScreenUtils;
import java.util.Objects;
/**
* 统一提示框
*/
public class CommonDialog extends Dialog {
/**
* 显示的图片
*/
private ImageView imageIv;
/**
* 显示的标题
*/
private TextView titleTv;
/**
* 显示的消息
*/
private TextView messageTv;
/**
* 确认和取消按钮
*/
private TextView negtiveBn, positiveBn;
public CommonDialog(Context context) {
super(context, R.style.CustomDialog);
}
/**
* 都是内容数据
*/
private String message;
private String title;
private String positive, negtive;
private int imageResId = -1;
/**
* 底部是否只有一个按钮
*/
private boolean isSingle = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_tips);
//按空白处能取消动画
setCanceledOnTouchOutside(true);
//初始化界面控件
initView();
//初始化界面数据
refreshView();
//初始化界面控件的事件
initEvent();
getWindow().setGravity(Gravity.BOTTOM);//设置显示在底部
WindowManager windowManager=getWindow().getWindowManager();
Display display= windowManager.getDefaultDisplay();
WindowManager.LayoutParams layoutParams=getWindow().getAttributes();
layoutParams.width=display.getWidth();//设置Dialog的宽度为屏幕宽度
layoutParams.height = display.getHeight();
getWindow().setAttributes(layoutParams);
}
/**
* 初始化界面的确定和取消监听器
*/
private void initEvent() {
//设置确定按钮被点击后,向外界提供监听
positiveBn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onClickBottomListener != null) {
onClickBottomListener.onPositiveClick();
}
}
});
//设置取消按钮被点击后,向外界提供监听
negtiveBn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onClickBottomListener != null) {
onClickBottomListener.onNegtiveClick();
}
}
});
}
/**
* 初始化界面控件的显示数据
*/
private void refreshView() {
//如果用户自定了title和message
if (!TextUtils.isEmpty(title)) {
titleTv.setText(title);
titleTv.setVisibility(View.VISIBLE);
} else {
titleTv.setVisibility(View.GONE);
}
if (!TextUtils.isEmpty(message)) {
messageTv.setText(message);
}
//如果设置按钮的文字
if (!TextUtils.isEmpty(positive)) {
positiveBn.setText(positive);
} else {
positiveBn.setText("确定");
}
if (!TextUtils.isEmpty(negtive)) {
negtiveBn.setText(negtive);
} else {
negtiveBn.setText("取消");
}
if (imageResId != -1) {
imageIv.setImageResource(imageResId);
imageIv.setVisibility(View.VISIBLE);
} else {
imageIv.setVisibility(View.GONE);
}
/**
* 只显示一个按钮的时候隐藏取消按钮,回掉只执行确定的事件
*/
if (isSingle) {
negtiveBn.setVisibility(View.GONE);
} else {
negtiveBn.setVisibility(View.VISIBLE);
}
}
@Override
public void show() {
super.show();
refreshView();
Objects.requireNonNull(this.getWindow()).getDecorView().setBackgroundColor(Color.parseColor("#00000000"));
// Objects.requireNonNull(this.getWindow()).setLayout(ScreenUtils.getScreenWidth(), ScreenUtils.getScreenHeight() - BarUtils.getActionBarHeight());
// getWindow().setGravity(Gravity.BOTTOM);//设置显示在底部
}
/**
* 初始化界面控件
*/
private void initView() {
negtiveBn = findViewById(R.id.cancel_btn);
positiveBn = findViewById(R.id.submit_btn);
titleTv = findViewById(R.id.tip_tv);
messageTv = findViewById(R.id.message_tv);
imageIv = findViewById(R.id.image);
}
/**
* 设置确定取消按钮的回调
*/
public OnClickBottomListener onClickBottomListener;
public CommonDialog setOnClickBottomListener(OnClickBottomListener onClickBottomListener) {
this.onClickBottomListener = onClickBottomListener;
return this;
}
public interface OnClickBottomListener {
/**
* 点击确定按钮事件
*/
public void onPositiveClick();
/**
* 点击取消按钮事件
*/
public void onNegtiveClick();
}
public String getMessage() {
return message;
}
public CommonDialog setMessage(String message) {
this.message = message;
return this;
}
public String getTitle() {
return title;
}
public CommonDialog setTitle(String title) {
this.title = title;
return this;
}
public String getPositive() {
return positive;
}
public CommonDialog setPositive(String positive) {
this.positive = positive;
return this;
}
public String getNegtive() {
return negtive;
}
public CommonDialog setNegtive(String negtive) {
this.negtive = negtive;
return this;
}
public int getImageResId() {
return imageResId;
}
public boolean isSingle() {
return isSingle;
}
public CommonDialog setSingle(boolean single) {
isSingle = single;
return this;
}
public CommonDialog setImageResId(int imageResId) {
this.imageResId = imageResId;
return this;
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/update_con_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<View
android:id="@+id/back_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@drawable/top_corners_12dp_white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHeight_percent="0.33"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<TextView
android:id="@+id/tip_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="提示"
android:textColor="@color/colorMainTitleBody"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintLeft_toLeftOf="@id/back_view"
app:layout_constraintRight_toRightOf="@id/back_view"
app:layout_constraintTop_toTopOf="@id/back_view" />
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="10dp"
android:maxWidth="150dp"
android:maxHeight="150dp"
android:visibility="gone"
android:layout_marginTop="10dp"
app:layout_constraintTop_toBottomOf="@id/tip_tv"
app:layout_constraintLeft_toLeftOf="@id/back_view"
app:layout_constraintRight_toRightOf="@id/back_view"
/>
<TextView
android:id="@+id/message_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="发现新版本"
android:textColor="@color/colorMainTitleBody"
android:textSize="16sp"
app:layout_constraintLeft_toLeftOf="@id/back_view"
app:layout_constraintRight_toRightOf="@id/back_view"
app:layout_constraintTop_toBottomOf="@id/image" />
<TextView
android:id="@+id/cancel_btn"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="32dp"
android:background="@drawable/four_corners_5dp_line"
android:gravity="center"
android:text="@string/cancel"
android:textColor="@color/colorMainStyle"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="14:5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/submit_btn"
app:layout_constraintWidth_percent="0.375" />
<TextView
android:id="@+id/submit_btn"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="32dp"
android:background="@drawable/four_corners_5dp"
android:gravity="center"
android:text="@string/update"
android:textColor="@color/cardview_light_background"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="14:5"
app:layout_constraintLeft_toRightOf="@id/cancel_btn"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintWidth_percent="0.375"
tools:ignore="PrivateResource" />
</androidx.constraintlayout.widget.ConstraintLayout>
style定义:
<style name="CustomDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@drawable/dialog_confirm</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:background">#00000000</item>
</style>
圆角背景图:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
android:width="40dp"
android:height="20dp" />
<solid android:color="#FFFFFF" />
<corners android:radius="12dp" />
</shape>
本文地址:https://blog.csdn.net/StudyOfAndroid/article/details/107413020
推荐阅读
-
Android自定义控件动态显示时间 年月日 时分秒
-
Android实现自定义带文字和图片Button的方法
-
Android9.0 SystemUI 快速设置栏增加自定义字体大小栏
-
Android 中自定义Dialog样式的Activity点击空白处隐藏软键盘功能(dialog不消失)
-
Android开发必知 九种对话框的实现方法
-
android设置自定义view的宽高达到设计图比例一致的效果
-
Android ServiceManager注册自定义service
-
Android ServiceManager注册自定义service
-
android自定义等级评分圆形进度条
-
Android自定义View实现选座功能