如何创建android的自定义对话框? [英] How to create a Custom Dialog box in android?

查看:163
本文介绍了如何创建android的自定义对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to create a custom dialog box like below

我曾尝试下面的东西。

  1. 我创建的 AlertDialog.Builder 的子类,并使用了自定义的标题和自定义内容的浏览和使用的,但结果并不如预期。

  1. I created a subclass of AlertDialog.Builder and used a custom Title and Custom Content View and used that but the result was not as expected.

另一种尝试是子类的 DialogFragment 和自定义并不如预期,但结果里面onCreateDialog对话框。

Another attempt was to subclass DialogFragment and customize the dialog inside onCreateDialog that but result was not as expected.

然后我试图用一个简单的对话框类。结果并不如预期。

Then I tried using a plain Dialog class. The result was not as expected.

在这三种情况下,问题是,当我忽略标题查看对话框并不如预期的大小,当我用标题查看结果是周围有内容视图中的粗边框(这真的很糟糕) 。现在,我有两个问题在我的脑海...

In all three cases, the problem is when I overlook the title view the size of the dialog is not as expected and when I use Title view the result is there is a thick border around the content view (which really looks bad). Now I have two questions in my mind...

  1. 我怎样才能做到这一点?正如我已经尝试过这么多的事情,直接回答会更AP preciated。

  1. How can I achieve that? As I have already tried so many things, a direct answer will be more appreciated.

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

What is the best way to show an error or alert dialog in an android app?

修改 Android开发者文档的建议,我们应使用DialogFragments或对话框的显示错误/警报消息的用户。然而,在一个点上,他们说...

EDIT Android Developer Documentation recommends that we should use either DialogFragments or Dialogs for showing Error / Alert Messages to the user. However at one point they say ...

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

Tip: If you want a custom dialog, you can instead display an Activity as a dialog instead of using the Dialog APIs. Simply create an activity and set its theme to Theme.Holo.Dialog in the manifest element.

什么是是,什么意思?是不是过多地使用活动只是为了显示错误信息???

推荐答案

在这里,我已经创建简单的对话。

Here i have create simple Dialog.

这样的:

like :

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>

您必须扩展对话框实现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();
  }
}

如何调用对话?

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

更新

在很长一段时间我的一个朋友问我,使弯曲的透明背景的形状对话框。所以,在这里我有实现它。

Updates

After long time one of my friend asked me to make curved shape dialog with transparent background. So, Here i have implement it.

让你需要创建一个单独的弯曲的形状 curve_shap.XML 如下,

To Make curved shape you need to create separate curve_shap.XML as below,

<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

Now, add this curve_shap.XML in to your main view Layout. In my case i have use 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>

如何调用呢?

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

我希望它为你工作。

I hope its work for you.

这篇关于如何创建android的自定义对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆