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

在此处输入图像描述

我试过以下方法.

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

  2. 另一种try 是派生DialogFragment的子类并定制onCreateDialog内的对话框,但结果与预期不符.

  3. 然后我try 使用普通的Dialog类.结果出乎意料.

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

  1. 我怎样才能做到这一点?因为我已经try 了很多东西,所以如果能直接回答,我会更加感激.

  2. 在android应用程序中显示错误或alert 对话框的最佳方式是什么?

EDIT Android Developer Documentation建议我们使用DialogFragments或Dialog向用户显示错误/alert 消息.然而,在某一时刻,他们说...

提示:如果需要自定义对话框,可以将活动显示为对话框,而不是使用对话框API.只需创建一个活动并将其主题设置为主题即可.全息. list 元素中的对话框.

What is the meaning of that? Isn't it too much to use an Activity just for showing an error message???

推荐答案

在这里,我创建了一个简单的对话框,如:

Dialog Box

custom_dialog.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="80dp"
    android:background="#3E80B4"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txt_dia"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="10dp"
        android:text="Do you realy want to exit ?"
        android:textColor="@android:color/white"
        android:textSize="15dp"
        android:textStyle="bold"/>
    

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#3E80B4"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_yes"
            android:layout_width="100dp"
            android:layout_height="30dp"
            android:background="@android:color/white"
            android:clickable="true"
            android:text="Yes"
            android:textColor="#5DBCD2"
            android:textStyle="bold" />

        <Button
            android:id="@+id/btn_no"
            android:layout_width="100dp"
            android:layout_height="30dp"
            android:layout_marginLeft="5dp"
            android:background="@android:color/white"
            android:clickable="true"
            android:text="No"
            android:textColor="#5DBCD2"
            android:textStyle="bold" />
    </LinearLayout>

</LinearLayout>

你得走到extends Dialogimplements OnClickListener

public class CustomDialogClass extends Dialog implements
    android.view.View.OnClickListener {

  public Activity c;
  public Dialog d;
  public Button yes, no;

  public CustomDialogClass(Activity a) {
    super(a);
    // TODO Auto-generated constructor stub
    this.c = a;
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.custom_dialog);
    yes = (Button) findViewById(R.id.btn_yes);
    no = (Button) findViewById(R.id.btn_no);
    yes.setOnClickListener(this);
    no.setOnClickListener(this);

  }

  @Override
  public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btn_yes:
      c.finish();
      break;
    case R.id.btn_no:
      dismiss();
      break;
    default:
      break;
    }
    dismiss();
  }
}

How to Call Dialog ?

R.id.TXT_Exit:
CustomDialogClass cdd=new CustomDialogClass(Values.this);
cdd.show();  

Updates

过了很长一段时间,我的一个朋友让我做一个透明背景的弧形对话框.所以,我在这里实现了它.

enter image description here

要制作一个曲线形状,您需要创建一个单独的curve_shap.XML,如下所示,

<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="#000000" />

    <stroke
        android:width="2dp"
        android:color="#ffffff" />

    <corners
        android:bottomLeftRadius="20dp"
        android:bottomRightRadius="20dp"
        android:topLeftRadius="20dp"
        android:topRightRadius="20dp" />

</shape>

现在,将这curve_shap.XML添加到您的主视图布局中.在我的例子中,我用的是LinearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="80dp"
        android:background="@drawable/curve_shap"
        android:orientation="vertical" >
...
</LinearLayout>

How to call this ?

CustomDialogClass cdd = new CustomDialogClass(MainActivity.this);
cdd.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
cdd.show();

我希望这对你有用.

Android相关问答推荐

编写Inbox需要具有匹配兼容性的Kotlin版本

无法列出目录中的文件'

Jetpack Compose-如何使用值动画直接控制其他动画

Android手柄注射周期错误,多个模块引用一个核心模块

有没有什么方法可以让Beeware在安卓手机上显示图片?

为什么可以';我不能直接在RecyclerView.ViewHolder中访问视图ID吗?

Android 不在后台更新位置

Android Jetpack Compose全宽度抽屉优化

Jetpack Compose:如何绘制异形边框?

设置背景图片组成Column

Android CompanionDeviceManager 永远找不到任何附近的蓝牙设备

如何在 React Native 中调试网络响应

我该怎么做文本计时器

如何放置在柱子的角落(底端)

在 Kotlin 中打开新片段时如何对当前片段应用更改?

Android Jetpack Compose - 每次文本字段值更改时,可组合函数都会重新组合

使用 Jetpack Compose 时,如何以简单的方式在 Color.kt 中定义 colored颜色 ?

如何在 TextInputEdit 中调整可绘制对象的大小

如何在 Jetpack Compose 中禁用 TabRow 或 Tab 中的涟漪效应?

关于launchWhenX和repeatOnLifecycle的问题