相关疑难解决方法(0)

如何在android中创建自定义对话框?

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

在此输入图像描述

我尝试过以下的事情.

  1. 我创建了AlertDialog.Builder的子类,并使用了自定义标题和自定义内容视图并使用了它,但结果并不像预期的那样.

  2. 另一种尝试是继承DialogFragment并在onCreateDialog中自定义对话框,但结果不是预期的.

  3. 然后我尝试使用普通的Dialog类.结果并不像预期的那样.

在所有三种情况下,问题是当我忽略标题视图时,对话框的大小不是预期的,当我使用标题视图时,结果是内容视图周围有一个粗边框(实际上看起来很糟糕).现在我脑子里有两个问题......

  1. 我怎样才能做到这一点?由于我已经尝试了很多东西,因此我们将更加赞赏直接的答案.

  2. 在Android应用程序中显示错误或警告对话框的最佳方法是什么?

编辑 Android开发人员文档建议我们应该使用DialogFragments或Dialogs向用户显示错误/警报消息.但有一次他们说......

提示:如果需要自定义对话框,则可以将"活动"显示为对话框,而不是使用"对话框API".只需创建一个活动,并将其主题设置为清单元素中的Theme.Holo.Dialog.

那是什么意思?使用Activity只是为了显示错误消息是不是太多了?

android android-dialog android-dialogfragment

333
推荐指数
10
解决办法
56万
查看次数

如何更改AlertDialog的主题

我想知道是否有人可以帮助我.我正在尝试创建自定义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)
  • color_panel_background.9.png位于drawable文件夹中.这也可以在Android SDK res文件夹中找到.

以下是主要活动.

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)

alert android themes dialog

230
推荐指数
9
解决办法
33万
查看次数

Android:如何在不使用自定义布局的情况下更改 AlertDialog 标题文本颜色和背景颜色?

我想在不使用自定义布局的情况下更改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)

用下面我试图线,但它总是返回nullfindViewById

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)

android textcolor background-color android-alertdialog

10
推荐指数
2
解决办法
1万
查看次数