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

我尝试过以下的事情.
我创建了AlertDialog.Builder的子类,并使用了自定义标题和自定义内容视图并使用了它,但结果并不像预期的那样.
另一种尝试是继承DialogFragment并在onCreateDialog中自定义对话框,但结果不是预期的.
然后我尝试使用普通的Dialog类.结果并不像预期的那样.
在所有三种情况下,问题是当我忽略标题视图时,对话框的大小不是预期的,当我使用标题视图时,结果是内容视图周围有一个粗边框(实际上看起来很糟糕).现在我脑子里有两个问题......
我怎样才能做到这一点?由于我已经尝试了很多东西,因此我们将更加赞赏直接的答案.
在Android应用程序中显示错误或警告对话框的最佳方法是什么?
编辑 Android开发人员文档建议我们应该使用DialogFragments或Dialogs向用户显示错误/警报消息.但有一次他们说......
提示:如果需要自定义对话框,则可以将"活动"显示为对话框,而不是使用"对话框API".只需创建一个活动,并将其主题设置为清单元素中的Theme.Holo.Dialog.
那是什么意思?使用Activity只是为了显示错误消息是不是太多了?
我有一个AlertDialog和它的消息显示,但文本的颜色是白色.它与背景融为一体.我试过改变主题,但它不起作用.如何更改消息的颜色?
相关代码:
AlertDialog.Builder builder;
builder = new AlertDialog.Builder(MainActivityGame.this);
builder.setTitle("Name");
builder.setMessage("Are you ");
builder.setCancelable(false);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Submit default name, go home
boolean isInserted = myDb.insertData(defaultName, triesTaken, difficultyText);
if (isInserted) {
Toast.makeText(MainActivityGame.this, "Your name was submitted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivityGame.this, "Error, your name wasn't submitted\n Have you entered a default name?\n Go to Settings/Default Name to set it up", Toast.LENGTH_SHORT).show();
}
Intent intent = new Intent(MainActivityGame.this, MainActivity.class);
startActivity(intent);
} …Run Code Online (Sandbox Code Playgroud) 阅读MaterialComponents 主题警报对话框按钮和https://medium.com/@lcdsmao/material-design-custom-alert-dialog-5a9cab3ade11我设置AlertDialog了新材料主题的按钮和文本颜色。
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<!-- AlertDialog -->
<item name="materialAlertDialogBodyTextStyle">@style/MaterialAlertDialogTextTheme</item>
<item name="materialAlertDialogTheme">@style/MaterialAlertDialogButtonsTheme</item>
</style>
<!-- AlertDialog text -->
<style name="MaterialAlertDialogTextTheme" parent="MaterialAlertDialog.MaterialComponents.Body.Text">
<item name="android:textColor">@color/colorPrimary</item>
<item name="android:colorAccent">@color/colorPrimary</item>
<item name="colorAccent">@color/colorPrimaryitem>
<item name="android:textSize">14sp</item>
<item name="android:textStyle">bold</item>
</style>
<!-- AlertDialog buttons -->
<style name="MaterialAlertDialogButtonsTheme" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="buttonBarPositiveButtonStyle">@style/AlertDialog.Button</item>
...
Run Code Online (Sandbox Code Playgroud)
创建AlertDialogFragment使用后
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
super.onCreateDialog(savedInstanceState)
return MaterialAlertDialogBuilder(context!!).apply {
...
}.create()
} …Run Code Online (Sandbox Code Playgroud) android android-alertdialog material-design material-components material-components-android