[英]android add icon to the edge of custom alert dialog
我想在我的自定義警報對話框中添加一個圖標
我希望將其添加到對話框的邊緣,如下圖所示。
那么我怎樣才能做到這一點
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:background="@drawable/custom_alert_dialog"
android:layout_height="match_parent">
<TextView
android:background="@color/Blue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Warning"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<Button
android:id="@+id/btn_confirm"
android:layout_width="100dp"
android:layout_height="30dp"
/>
<Button
android:id="@+id/btn_quit"
android:layout_width="100dp"
android:layout_height="30dp"
/>
</LinearLayout>
你應該使用圓形圖像視圖
2.添加此代碼
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.makeramen.roundedimageview .RoundedImageView android:layout_width="100dp" android:layout_height="100dp" android:elevation="2dp" android:background="@drawable/dark_blue_circle" android:layout_centerHorizontal="true"/>
<com.makeramen.roundedimageview.RoundedImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginTop="10dp"
android:elevation="2dp"
android:background="@drawable/grey_circle"
android:layout_centerHorizontal="true"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:background="@drawable/rounded_white_rectangle"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_marginTop="80dp">
<TextView
android:layout_marginTop="30dp"
android:textColor="@color/blue_dark"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Warning"
android:textStyle="bold"
android:textAlignment="center"
android:textSize="40dp"/>
<TextView
android:layout_marginTop="30dp"
android:textColor="@color/blue_dark"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="You just display this"
android:textAlignment="center"
android:textSize="40dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="Done"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
android:textSize="40dp"
android:textColor="white"/>
</LinearLayout>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
我將在回放中添加第 3 步
Kotlin代碼
class CustomOneDialog : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val binding = DialogCustomOneBinding.inflate(layoutInflater)
binding.btnDone.setOnClickListener {
dialog?.dismiss()
}
val dialog = MaterialAlertDialogBuilder(
requireActivity(),
R.style.MaterialAlertDialog_CustomBorders
).apply {
setView(binding.root)
setOnKeyListener { _, keyCode, keyEvent ->
if (keyCode == KeyEvent.KEYCODE_BACK && keyEvent.action == KeyEvent.ACTION_UP) {
dismiss()
true
} else false
}
}.create()
dialog.getWindow()?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
return dialog
}
}
布局
<?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">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:background="@drawable/dialog_custom_borders"
android:orientation="vertical"
android:paddingStart="16dp"
android:paddingTop="48dp"
android:paddingEnd="16dp"
android:paddingBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/dialog_congratulations_title"
android:textAppearance="@style/TextAppearance.MaterialComponents.Headline5"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="center"
android:text="@string/dialog_congratulations_summary"
android:textAppearance="@style/TextAppearance.MaterialComponents.Body1" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="16dp"
android:text="@android:string/ok"
android:textColor="@color/md_white_1000"
app:shapeAppearance="@style/ShapeAppearance.Capsule" />
</LinearLayout>
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/appCompatImageView"
android:layout_width="68dp"
android:layout_height="68dp"
android:layout_marginTop="-28dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/linearLayout"
app:srcCompat="@drawable/circle_rounded_filled" />
</androidx.constraintlayout.widget.ConstraintLayout>
帶有完成圖標的資源圈
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="UnusedResources">
<item>
<shape android:shape="oval">
<size
android:width="34dp"
android:height="34dp" />
<solid android:color="@color/md_light_green_A700" />
<stroke
android:width="2dp"
android:color="?android:colorBackgroundFloating" />
</shape>
</item>
<item
android:drawable="@drawable/ic_baseline_done_24"
android:gravity="center" />
</layer-list>
紐扣膠囊款式
<style name="ShapeAppearance.Capsule" parent="ShapeAppearance.MaterialComponents.SmallComponent">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">50%</item>
</style>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.