- 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>
- 调用
MyDialog myDialog = new MyDialog(MainActivity.this);
myDialog.show();
-
效果图:
image.png -
demo源代码:
https://gitee.com/cxyzy1/custom_dialog/tree/master/simpleCustDialog
安卓开发技术分享: https://www.jianshu.com/p/442339952f26
网友评论