我想创建一个自定义对话框如下
我尝试了以下几点。
我创建了AlertDialog的子类。生成器,并使用自定义标题和自定义内容视图,并使用,但结果不是预期的。
另一个尝试是子类DialogFragment和自定义对话框内的onCreateDialog,但结果不是预期的。
然后我尝试使用一个普通的Dialog类。结果并不如预期的那样。
在这三种情况下,问题是当我忽略标题视图时,对话框的大小不是预期的,当我使用标题视图时,结果是在内容视图周围有一个粗边框(这看起来真的很糟糕)。现在我脑子里有两个问题……
我怎样才能做到呢?因为我已经尝试了很多事情,直接的回答会更受欢迎。
在android应用程序中显示错误或警告对话框的最佳方式是什么?
编辑
Android开发者文档建议我们应该使用对话片段或对话框向用户显示错误/警报消息。然而,他们一度说……
提示:如果你想要一个自定义对话框,你可以将一个Activity显示为一个对话框,而不是使用对话框api。简单地创建一个活动,并将其主题设置为manifest元素中的theme . holo . dialog。
这是什么意思?仅仅是为了显示错误消息而使用Activity不是太过分了吗??
它是Alert Dialog的类,所以你可以从任何活动调用该类来重用代码。
public class MessageOkFragmentDialog extends DialogFragment {
Typeface Lato;
String message = " ";
String title = " ";
int messageID = 0;
public MessageOkFragmentDialog(String message, String title) {
this.message = message;
this.title = title;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View convertview = inflater.inflate(R.layout.dialog_message_ok_box, null);
Constants.overrideFonts(getActivity(), convertview);
Lato = Typeface
.createFromAsset(getActivity().getAssets(), "font/Lato-Regular.ttf");
TextView textmessage = (TextView) convertview
.findViewById(R.id.textView_dialog);
TextView textview_dialog_title = (TextView) convertview.findViewById(R.id.textview_dialog_title);
textmessage.setTypeface(Lato);
textview_dialog_title.setTypeface(Lato);
textmessage.setText(message);
textview_dialog_title.setText(title);
Button button_ok = (Button) convertview
.findViewById(R.id.button_dialog);
button_ok.setTypeface(Lato);
builder.setView(convertview);
button_ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
dismiss();
}
});
return builder.create();
}
}
相同的Xml文件是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:gravity="center_vertical|center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/blue_color"
android:gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:id="@+id/textview_dialog_title"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:gravity="center"
android:textColor="@color/white_color"
android:textSize="@dimen/txtSize_Medium" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/txt_white_color" />
<TextView
android:id="@+id/textView_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="@dimen/margin_20"
android:textColor="@color/txt_gray_color"
android:textSize="@dimen/txtSize_small" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/txt_white_color"
android:visibility="gone"/>
<Button
android:id="@+id/button_dialog"
android:layout_width="wrap_content"
android:layout_height="@dimen/margin_40"
android:layout_gravity="center"
android:background="@drawable/circular_blue_button"
android:text="@string/ok"
android:layout_marginTop="5dp"
android:layout_marginBottom="@dimen/margin_10"
android:textColor="@color/txt_white_color"
android:textSize="@dimen/txtSize_small" />
</LinearLayout>
</LinearLayout>
我正在发布我正在使用的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()
我希望,这将帮助你!
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();
}
});
}