製作一個客製化的dialog
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="26dp"
app:cardBackgroundColor="@android:color/white"
app:cardCornerRadius="10dp"
app:cardElevation="0dp"
tools:context="com.example.CustomAlertDialog">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:gravity="center"
android:minWidth="300dp"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:minHeight="36dp"
android:textColor="@color/gray4"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="標題" />
<ScrollView
android:id="@+id/sv_block_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@id/block_bottom_bar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_max="460dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_title"
app:layout_constraintWidth_min="300dp">
<TextView
android:id="@+id/tv_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:minHeight="40dp"
android:gravity="center_horizontal"
android:textColor="@color/gray6"
android:textSize="textSize14sp"
tools:text="訊息" />
</ScrollView>
<LinearLayout
android:id="@+id/block_bottom_bar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<Button
android:id="@+id/btn_negative"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minWidth="135dp"
android:minHeight="36dp"
android:text="@string/btn_cancel"
android:textColor="@color/blue5"
android:textSize="@dimen/textSize14sp" />
<Button
android:id="@+id/btn_positive"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minWidth="135dp"
android:minHeight="36dp"
android:text="@string/btn_determine"
android:textColor="@color/blue5"
android:textSize="@dimen/textSize14sp" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
class CustomAlertDialog(context: Context) : AlertDialog(context) {
private var mTitle: String? = null
private var mMessage: String? = null
private var mSpannedMessage: Spanned? = null
private var mPositiveText: String? = context.getString(R.string.btn_determine)
private var mNegativeText: String? = context.getString(R.string.btn_cancel)
private var mPositiveClickListener: View.OnClickListener = View.OnClickListener { dismiss() }
private var mNegativeClickListener: View.OnClickListener = View.OnClickListener { dismiss() }
private var mGravity = Gravity.CENTER
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.dialog_custom_alert)
window?.setBackgroundDrawableResource(android.R.color.transparent)
initView()
}
private fun initView() {
when (mTitle) {
null -> tv_title.visibility = View.GONE
else -> tv_title.text = mTitle
}
tv_message.gravity = mGravity
when {
mSpannedMessage != null -> tv_message.text = mSpannedMessage
mMessage == null -> sv_block_content.visibility = View.GONE
else -> tv_message.text = mMessage
}
if (mPositiveText == null) {
btn_positive.visibility = View.GONE
} else
btn_positive.text = mPositiveText
if (mNegativeText == null) {
btn_negative.visibility = View.GONE
} else
btn_negative.text = mNegativeText
btn_positive.setOnClickListener(mPositiveClickListener)
btn_negative.setOnClickListener(mNegativeClickListener)
}
////
//以下設定要在 dialog.show() 之前才有效果
////
fun setTitle(title: String?) {
mTitle = title
}
fun setMessage(message: String?) {
mMessage = message
}
//set .html 語法文字
fun setMessage(spanned: Spanned) {
mSpannedMessage = spanned
}
fun setGravity(gravity: Int) {
mGravity = gravity
}
/**
* @param positiveText: Positive 按鈕文字,若給 null 則隱藏按鈕
*/
fun setPositiveButtonText(positiveText: String?) {
mPositiveText = positiveText
}
/**
* @param negativeText: Negative 按鈕文字,若給 null 則隱藏按鈕
*/
fun setNegativeButtonText(negativeText: String?) {
mNegativeText = negativeText
}
fun setPositiveClickListener(positiveClickListener: View.OnClickListener) {
mPositiveClickListener = positiveClickListener
}
fun setNegativeClickListener(negativeClickListener: View.OnClickListener) {
mNegativeClickListener = negativeClickListener
}
}