我想创建一个自定义对话框,如下所示

我尝试过以下的事情.
我创建了AlertDialog.Builder的子类,并使用了自定义标题和自定义内容视图并使用了它,但结果并不像预期的那样.
另一种尝试是继承DialogFragment并在onCreateDialog中自定义对话框,但结果不是预期的.
然后我尝试使用普通的Dialog类.结果并不像预期的那样.
在所有三种情况下,问题是当我忽略标题视图时,对话框的大小不是预期的,当我使用标题视图时,结果是内容视图周围有一个粗边框(实际上看起来很糟糕).现在我脑子里有两个问题......
我怎样才能做到这一点?由于我已经尝试了很多东西,因此我们将更加赞赏直接的答案.
在Android应用程序中显示错误或警告对话框的最佳方法是什么?
编辑 Android开发人员文档建议我们应该使用DialogFragments或Dialogs向用户显示错误/警报消息.但有一次他们说......
提示:如果需要自定义对话框,则可以将"活动"显示为对话框,而不是使用"对话框API".只需创建一个活动,并将其主题设置为清单元素中的Theme.Holo.Dialog.
那是什么意思?使用Activity只是为了显示错误消息是不是太多了?
我想知道是否有人可以帮助我.我正在尝试创建自定义AlertDialog.为此,我在styles.xml中添加了以下代码行
<resources>
<style name="CustomAlertDialog" parent="android:Theme.Dialog.Alert">
<item name="android:windowBackground">@drawable/color_panel_background</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
以下是主要活动.
package com.customdialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
public class CustomDialog extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.setTheme(R.style.CustomAlertDialog);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("HELLO!");
builder .setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int …Run Code Online (Sandbox Code Playgroud) 我想在不使用自定义布局的情况下更改AlertDialog 标题颜色和背景颜色。我的要求,
我试过下面的代码,但不能工作。
final CharSequence[] items = {" Visiting Card", "Prescription Letter"};
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setMessage(message)
.setTitle(title).setCancelable(false);
builder.setItems(items, (dialog, item) -> {
});
AlertDialog dialog = builder.create();
dialog.show();
int textViewId = dialog.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
TextView tv = dialog.findViewById(textViewId); // It always returns null
if (tv != null) {
tv.setTextColor(activity.getResources().getColor(R.color.white));
tv.setBackgroundColor(activity.getResources().getColor(R.color.colorPrimary));
}
Run Code Online (Sandbox Code Playgroud)
用下面我试图线,但它总是返回null的findViewById,
int textViewId = dialog.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
TextView tv = dialog.findViewById(textViewId);
Run Code Online (Sandbox Code Playgroud)
我也尝试使用,style但它只更改标题文本颜色,
<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">@color/colorAccent</item> …Run Code Online (Sandbox Code Playgroud)