安卓自定义dialog

作者: 蓝不蓝编程 | 来源:发表于2018-11-28 17:46 被阅读10次
    1. MyDialog.java
    public class MyDialog extends Dialog {
        public MyDialog(@NonNull Context context) {
            super(context);
            setContentView(R.layout.dialog_layout);
        }
    }
    

    2.dialog_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="300dp"
        android:layout_height="200dp">
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="218dp"
            android:layout_height="59dp"
            android:layout_marginTop="84dp"
            android:text="我是定制dialog"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.303"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:text="OK"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/button3"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent" />
    
        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:text="Cancel"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/button2" />
    </android.support.constraint.ConstraintLayout>
    
    1. 调用
     MyDialog myDialog = new MyDialog(MainActivity.this);
                myDialog.show();
    
    1. 效果图:


      image.png
    2. demo源代码:
      https://gitee.com/cxyzy1/custom_dialog/tree/master/simpleCustDialog

    安卓开发技术分享: https://www.jianshu.com/p/442339952f26

    相关文章

      网友评论

        本文标题:安卓自定义dialog

        本文链接:https://www.haomeiwen.com/subject/gyszqqtx.html