【问题标题】:Android Custom Dialog Corner RadiusAndroid 自定义对话框角半径
【发布时间】:2020-08-13 16:13:08
【问题描述】:

我在我的 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,我可以根据需要设置任意宽度。

【问题讨论】:

    标签: java android android-layout android-dialogfragment


    【解决方案1】:

    您不需要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 中创建它。检查这个answer for more details

    【讨论】:

    • 是否可以通过编程方式将角半径设置为主题?我必须根据用户输入设置圆角半径
    • 这是一个简单但有效的解决方案。尽管它没有回答 OP 的问题,但它确实为 DialogFragment 添加了边框半径。
    • 只是为了向 Gabriele 答案添加一个细节:从您继承的样式中创建 RoundedCornersDialog,覆盖 textColor 和其他内容,如果您之前在对话框中有文本并且在应用样式之后,文本消失,或者其他一些元素正在改变颜色,请确保您定义了要在样式中保持不变的属性。希望它可以帮助某人。
    【解决方案2】:

    您可以创建一个可绘制对象,然后将其作为视图的背景。

    您可以使用以下代码来制作圆角 - 只需创建一个新的可绘制对象并

    <?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>
    

    您还可以单独指定每个角的半径

    【讨论】:

    • 如果我想使用这个文件以编程方式设置圆角半径,我该怎么做?我必须根据用户输入更改拐角半径。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多