[英]Android Custom Dialog Corner Radius
我在我的 android 應用程序中填充了一個自定義對話框。 對話框的布局是這樣的:
<?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"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/parent"
app:cardBackgroundColor="@android:color/white"
app:cardCornerRadius="15dp"
app:cardElevation="3dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="@+id/interior_layout"
android:orientation="vertical">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:layout_gravity="center_horizontal"
android:visibility="gone"
android:id="@+id/imageView"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/message"
android:textSize="14sp"
android:textColor="@android:color/black"
android:visibility="gone"
android:layout_marginTop="10dp"
android:text="Hello"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
這是我膨脹對話框的代碼:
Dialog alertDialog = new Dialog(activity);
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialogView = LayoutInflater.from(activity).inflate(R.layout.layout_progress_dialog, null);
alertDialog.setContentView(dialogView);
Objects.requireNonNull(alertDialog.getWindow()).setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
但是,沒有設置拐角半徑。 棱角依然鋒利。 我嘗試使用警報對話框,但我遇到了它覆蓋整個寬度的問題(就像進度對話框一樣)。 使用 Dialog,我可以擁有所需的寬度。
您不需要CardView
來獲取角半徑。
只需使用getTheme
方法:
class RoundedDialog: DialogFragment() {
override fun getTheme() = R.style.RoundedCornersDialog
override fun onCreateView(...): View? {
val v: View = inflater.inflate(R.layout...., container, false)
return v
}
//...
}
和:
<style name="RoundedCornersDialog" parent="@style/Theme.MaterialComponents.Dialog">
<item name="dialogCornerRadius">16dp</item>
</style>
如果您想使用AlertDialog
,您可以在onCreateDialog
中創建它。 檢查此答案以獲取更多詳細信息。
您可以 go 創建可繪制對象,然后將其作為視圖的背景。
您可以使用以下代碼制作圓角 - 只需創建一個新的可繪制對象並
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="1dp"
android:color="@color/colorPrimaryDark" />
<solid android:color="#2ED771" />
<padding
android:left="1dp"
android:right="1dp"/>
<corners android:radius="15dp" />
</shape>
有了這個,您還可以獨立指定每個角的半徑
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.