// 构建Dialog
val dialog = Dialog(context)
val contentView: View =
LayoutInflater.from(view.context).inflate(R.layout.dialog_wish_product, null)
// 获取自定义View里的控件并传值
val tvWishValue: TextView = contentView.findViewById(R.id.tv_wish_value)
tvWishValue.text = "1980"
val textView: TextView = contentView.findViewById(R.id.tv_back)
textView.setOnClickListener { dialog.dismiss() }
dialog.setContentView(contentView)
dialog.setCanceledOnTouchOutside(true)
dialog.setCancelable(true);
val window: Window? = dialog.window
window?.let {
window.decorView.setPadding(0, 0, 0, 0) // 设置弹窗跟原始padding为0
val lp: WindowManager.LayoutParams? = window.attributes
lp?.let {
it.gravity = Gravity.CENTER
it.width = WindowManager.LayoutParams.MATCH_PARENT
it.height = WindowManager.LayoutParams.MATCH_PARENT
it.horizontalMargin = 0F
window.attributes = it
}
window.decorView.minimumWidth = view.context.resources.displayMetrics.widthPixels
window.decorView.setBackgroundColor(Color.TRANSPARENT) // 颜色要设置,不然不能全屏,所以设置透明
}
dialog.show()
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#B3000000">
<LinearLayout
android:layout_width="275dp"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="50dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="112dp"
android:background="@mipmap/wish_product_dialog_head">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="17dp"
android:text="@string/my_wish"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="85dp"
android:layout_marginStart="10dp"
android:layout_marginTop="14dp"
android:layout_marginEnd="10dp"
android:background="@mipmap/wish_product_dialog_box">
<ImageView
android:id="@+id/iv_img"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginStart="14dp"
android:contentDescription="@string/cover"
android:src="@mipmap/bbb"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_marginStart="7dp"
android:layout_marginTop="14dp"
android:layout_marginEnd="21dp"
android:maxLines="2"
android:src="@mipmap/bbb"
android:text="连帽加厚棉衣男冬季外套 潮牌港风军绿大衣"
android:textColor="@color/black"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toRightOf="@id/iv_img"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toEndOf="@id/iv_img"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_wish_value_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="7dp"
android:layout_marginEnd="21dp"
android:layout_marginBottom="17dp"
android:text="@string/wish_value"
android:textColor="@color/black"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@id/iv_img" />
<TextView
android:id="@+id/tv_wish_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="7dp"
android:layout_marginEnd="21dp"
android:layout_marginBottom="17dp"
android:text="@string/wish_value"
android:textColor="@color/c1"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@id/tv_wish_value_text" />
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="@+id/tv_tip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="17dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="17dp"
android:textColor="@color/text_color1" />
<TextView
android:layout_width="match_parent"
android:layout_height="34dp"
android:layout_marginStart="20dp"
android:layout_marginTop="26dp"
android:layout_marginEnd="20dp"
android:background="@drawable/btn_bg1"
android:gravity="center"
android:text="@string/start_wish"
android:textColor="@color/white"
android:textSize="15sp" />
<TextView
android:id="@+id/tv_back"
android:layout_width="match_parent"
android:layout_height="34dp"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="17dp"
android:background="@drawable/btn_bg1"
android:gravity="center"
android:text="@string/back"
android:textColor="@color/white"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
|