OGeek|极客世界-中国程序员成长平台

标题: android - 如何在android中创建自定义对话框? [打印本页]

作者: 菜鸟教程小白    时间: 2022-8-1 01:19
标题: android - 如何在android中创建自定义对话框?

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

enter image description here

我尝试了以下事情。

  • 我创建了 的子类AlertDialog.Builder 并使用了自定义标题和自定义内容 View 并使用了它,但结果不如预期。
  • 另一种尝试是子类 对话 fragment 并自定义 onCreateDialog 中的对话框,但结果与预期不符。
  • 然后我尝试使用普通的 对话类(class)。结果并不如预期。

  • 在所有三种情况下,问题是当我忽略标题 View 时,对话框的大小不符合预期,当我使用标题 View 时,结果是内容 View 周围有一个粗边框(看起来很糟糕)。现在我脑子里有两个问题...
  • 我怎样才能做到这一点?由于我已经尝试了很多事情,因此将更加感谢直接回答。
  • 在 android 应用程序中显示错误或警报对话框的最佳方式是什么?

  • 编辑
    Android Developer Documentation建议我们应该使用 DialogFragments 或 Dialogs 向用户显示错误/警报消息。然而,他们有一次说......

    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.



    那是什么意思?仅仅为了显示错误信息而使用 Activity 是不是太过分了???



    Best Answer-推荐答案


    在这里,我创建了一个简单的对话框,例如:
    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"
        androidrientation="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"
            androidrientation="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();
      }
    }
    
    如何调用对话框?
    R.id.TXT_Exit:
    CustomDialogClass cdd=new CustomDialogClass(Values.this);
    cdd.show();  
    
    更新
    很长一段时间后,我的一个 friend 要求我制作一个带有透明背景的曲线形状对话框。所以,在这里我已经实现了它。
    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"
            androidrientation="vertical" >
    ...
    </LinearLayout>
    
    这个怎么称呼?
    CustomDialogClass cdd = new CustomDialogClass(MainActivity.this);
    cdd.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    cdd.show();
    
    我希望这对你有用。

    关于android - 如何在android中创建自定义对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13341560/






    欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4