我想创建一个自定义对话框如下
我尝试了以下几点。
我创建了AlertDialog的子类。生成器,并使用自定义标题和自定义内容视图,并使用,但结果不是预期的。
另一个尝试是子类DialogFragment和自定义对话框内的onCreateDialog,但结果不是预期的。
然后我尝试使用一个普通的Dialog类。结果并不如预期的那样。
在这三种情况下,问题是当我忽略标题视图时,对话框的大小不是预期的,当我使用标题视图时,结果是在内容视图周围有一个粗边框(这看起来真的很糟糕)。现在我脑子里有两个问题……
我怎样才能做到呢?因为我已经尝试了很多事情,直接的回答会更受欢迎。
在android应用程序中显示错误或警告对话框的最佳方式是什么?
编辑
Android开发者文档建议我们应该使用对话片段或对话框向用户显示错误/警报消息。然而,他们一度说……
提示:如果你想要一个自定义对话框,你可以将一个Activity显示为一个对话框,而不是使用对话框api。简单地创建一个活动,并将其主题设置为manifest元素中的theme . holo . dialog。
这是什么意思?仅仅是为了显示错误消息而使用Activity不是太过分了吗??
创建自定义警报对话框
cumstomDialog.xml
<ImageView
android:id="@+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_margin="5dp"
app:srcCompat="@drawable/error" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fontFamily="@font/muli_bold"
android:text="Title"
android:layout_marginTop="5dp"
android:textColor="@android:color/black"
android:textSize="15sp" />
<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:fontFamily="@font/muli_regular"
android:text="Message"
android:textColor="@android:color/black"
android:textSize="12dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/cancelBTN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_margin="5dp"
android:background="@drawable/bground_radius_button_white"
android:text="No"
android:textColor="@color/black" />
<Button
android:id="@+id/acceptBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="5dp"
android:background="@drawable/bground_radius_button"
android:text="Yes"
android:textColor="@color/white" />
</LinearLayout>
在你的活动上显示你的自定义对话框:
public void showDialog(String title, String des, int icon) {
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.custom_dialog);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Button cancelBTN = dialog.findViewById(R.id.cancelBTN);
Button acceptBTN = dialog.findViewById(R.id.acceptBtn);
TextView tvTitle = dialog.findViewById(R.id.title);
TextView tvDescription = dialog.findViewById(R.id.description);
ImageView ivIcon = dialog.findViewById(R.id.icon);
tvTitle.setText(title);
tvDescription.setText(des);
ivIcon.setImageResource(icon);
cancelBTN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
acceptBTN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
dialog.show();
}
像这样调用:
“标题”、“信息”、“警告”;
我正在发布我正在使用的kotlin代码,它对我来说很好。您还可以为对话框按钮设置单击侦听器。
这是我的XML代码:
layout_custom_alert_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layoutDirection="ltr"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:id="@+id/view6"
android:layout_width="match_parent"
android:layout_height="20dp"
android:background="@color/colorPrimary" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/view6"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp">
<TextView
android:id="@+id/txt_alert_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
tools:text="are you sure?"
android:textAlignment="center"
android:textColor="@android:color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_alert_positive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView2"
android:layout_marginTop="8dp"
android:background="@android:color/transparent"
tools:text="yes"
android:textColor="@color/colorPrimaryDark"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btn_alert_negative"
app:layout_constraintTop_toBottomOf="@+id/txt_alert_title" />
<Button
android:id="@+id/btn_alert_negative"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="@android:color/transparent"
tools:text="no"
android:textColor="@color/colorPrimaryDark"
app:layout_constraintEnd_toStartOf="@+id/btn_alert_positive"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txt_alert_title" />
</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>
mAlertDialog.kt
class mAlertDialog(context: Context) {
private val btn_positive : Button
private val btn_negative : Button
private val txt_alert_title : TextView
private val dialog : AlertDialog
init {
val view = LayoutInflater.from(context).inflate(R.layout.layout_custom_alert_dialog,null)
val dialog_builder = AlertDialog.Builder(context)
dialog_builder.setView(view)
btn_negative = view.findViewById(R.id.btn_alert_negative)
btn_positive = view.findViewById(R.id.btn_alert_positive)
txt_alert_title = view.findViewById(R.id.txt_alert_title)
dialog = dialog_builder.create()
}
fun show()
{
dialog.show()
}
fun setPositiveClickListener(listener :onClickListener)
{
btn_positive.setOnClickListener { v ->
listener.onClick(btn_positive)
dialog.dismiss()
}
}
fun setNegativeClickListener(listener: onClickListener)
{
btn_negative.setOnClickListener { v ->
listener.onClick(btn_negative)
dialog.dismiss()
}
}
fun setPoitiveButtonText(text : String)
{
btn_positive.text = text
}
fun setNegativeButtonText(text : String)
{
btn_negative.text = text
}
fun setAlertTitle(title : String)
{
txt_alert_title.text = title
}
}
用于点击监听器的接口:
onClickListener.kt
interface onClickListener{
fun onClick(view : View)
}
示例使用
val dialog = mAlertDialog(context)
dialog.setNegativeButtonText("no i dont")
dialog.setPoitiveButtonText("yes is do")
dialog.setAlertTitle("do you like this alert dialog?")
dialog.setPositiveClickListener(object : onClickListener {
override fun onClick(view: View) {
Toast.makeText(context, "yes", Toast.LENGTH_SHORT).show()
}
})
dialog.setNegativeClickListener(object : onClickListener {
override fun onClick(view: View) {
Toast.makeText(context, "no", Toast.LENGTH_SHORT).show()
}
})
dialog.show()
我希望,这将帮助你!
用这段代码正确显示自定义对话框的简单方法
dialog_choose_photo.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="wrap_content"
android:background="@android:color/transparent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/l_buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/_5sdp"
android:layout_marginRight="@dimen/_5sdp"
android:layout_marginBottom="@dimen/_5sdp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_dialog_smallround_white"
android:orientation="vertical">
<TextView
android:id="@+id/txt_camera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:paddingTop="@dimen/_10sdp"
android:paddingBottom="@dimen/_10sdp"
android:text="Take a Photo"
android:textColor="#3080C7"
android:textSize="18sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#F1F0F0" />
<TextView
android:id="@+id/txt_gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:paddingTop="@dimen/_10sdp"
android:paddingBottom="@dimen/_10sdp"
android:text="Choose from Gallery"
android:textColor="#3080C7"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_5sdp"
android:background="@drawable/bg_dialog_smallround_white"
android:orientation="vertical">
<TextView
android:id="@+id/txt_cancel_dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:paddingTop="@dimen/_10sdp"
android:paddingBottom="@dimen/_10sdp"
android:text="Cancel"
android:textColor="@color/colorPrimary"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
只要调用这个函数
public void openPhotoDialog() {
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_choose_photo);
dialog.setCancelable(true);
// Setting dialogview
final Window window = dialog.getWindow();
window.setLayout(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.BOTTOM;
window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
window.setDimAmount(0.5f);
window.setAttributes(wlp);
TextView txt_camera = dialog.findViewById(R.id.txt_camera);
TextView txt_gallery = dialog.findViewById(R.id.txt_gallery);
TextView txt_cancel_dialog = dialog.findViewById(R.id.txt_cancel_dialog);
txt_camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
dialog.dismiss();
}
});
txt_gallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
dialog.dismiss();
}
});
txt_cancel_dialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
dialog.dismiss();
}
});
dialog.show();
}
Bg_dialog_smallround_white.xml绘制资源
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/white" />
<corners
android:radius="@dimen/_5sdp" />
</shape>
public static void showCustomAlertDialog(Context context, String name,
String id, String desc, String fromDate, String toDate,
String resions) {
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.dialog, null);
alertDialogBuilder.setView(view);
alertDialogBuilder.setCancelable(false);
final AlertDialog dialog = alertDialogBuilder.create();
dialog.show();
txt_empId = (TextView) view.findViewById(R.id.txt_dialog_empcode);
txt_empName = (TextView) view.findViewById(R.id.txt_dialog_empname);
txt_desc = (TextView) view.findViewById(R.id.txt_dialog_desc);
txt_startDate = (TextView) view.findViewById(R.id.txt_dialog_startDate);
txt_resions = (TextView) view.findViewById(R.id.txt_dialog_endDate);
txt_empId.setTypeface(Utils.setLightTypeface(context));
txt_empName.setTypeface(Utils.setLightTypeface(context));
txt_desc.setTypeface(Utils.setLightTypeface(context));
txt_startDate.setTypeface(Utils.setLightTypeface(context));
txt_resions.setTypeface(Utils.setLightTypeface(context));
txt_empId.setText(id);
txt_empName.setText(name);
txt_desc.setText(desc);
txt_startDate.setText(fromDate + "\t to \t" + toDate);
txt_resions.setText(resions);
btn_accept = (Button) view.findViewById(R.id.btn_dialog_accept);
btn_reject = (Button) view.findViewById(R.id.btn_dialog_reject);
btn_cancel = (Button) view.findViewById(R.id.btn_dialog_cancel);
btn_accept.setTypeface(Utils.setBoldTypeface(context));
btn_reject.setTypeface(Utils.setBoldTypeface(context));
btn_cancel.setTypeface(Utils.setBoldTypeface(context));
btn_cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
}