Android自定义对话框圆角。

huangapple go评论76阅读模式
英文:

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));

然而,圆角半径没有设置成功。角仍然是尖锐的。我尝试使用AlertDialog,但在那里我遇到了问题,它覆盖了整个宽度(就像ProgressDialog一样)。使用Dialog,我可以根据需要设置宽度。

英文:

I am inflating a custom dialog in my android app. The layout of the dialog is this:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.cardview.widget.CardView xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:id=&quot;@+id/parent&quot;
    app:cardBackgroundColor=&quot;@android:color/white&quot;
    app:cardCornerRadius=&quot;15dp&quot;
    app:cardElevation=&quot;3dp&quot;&gt;
    
    &lt;LinearLayout
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_gravity=&quot;center_horizontal&quot;
        android:id=&quot;@+id/interior_layout&quot;
        android:orientation=&quot;vertical&quot;&gt;
        
        &lt;ImageView
            android:layout_width=&quot;80dp&quot;
            android:layout_height=&quot;80dp&quot;
            android:scaleType=&quot;centerCrop&quot;
            android:adjustViewBounds=&quot;true&quot;
            android:layout_gravity=&quot;center_horizontal&quot;
            android:visibility=&quot;gone&quot;
            android:id=&quot;@+id/imageView&quot;/&gt;
        
        &lt;TextView
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:id=&quot;@+id/message&quot;
            android:textSize=&quot;14sp&quot;
            android:textColor=&quot;@android:color/black&quot;
            android:visibility=&quot;gone&quot;
            android:layout_marginTop=&quot;10dp&quot;
            android:text=&quot;Hello&quot;
            android:layout_gravity=&quot;center_horizontal&quot;/&gt;
        
    &lt;/LinearLayout&gt;

&lt;/androidx.cardview.widget.CardView&gt;

This is my code to inflate the dialog:

        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));

However, the corner radius is not being set. The corners are still sharp. I tried using Alert Dialog but there I faced the problem that it covers the entire width (just like Progress Dialog). With Dialog, I can have the width as much as needed.

答案1

得分: 5

你不需要使用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中创建它。查看这个答案以获取更多详细信息

英文:

You don't need a CardView to obtain the corner radius.

Just use the getTheme method:

class RoundedDialog: DialogFragment() {

    override fun getTheme() = R.style.RoundedCornersDialog
    
    override fun onCreateView(...): View? {
       val v: View = inflater.inflate(R.layout...., container, false)
       return v
    }

    //...
}

with:

&lt;style name=&quot;RoundedCornersDialog&quot; parent=&quot;@style/Theme.MaterialComponents.Dialog&quot;&gt;
        &lt;item name=&quot;dialogCornerRadius&quot;&gt;16dp&lt;/item&gt;
&lt;/style&gt;

Android自定义对话框圆角。

If you want to use a AlertDialog you can create it in the onCreateDialog. Check this answer for more details.

答案2

得分: 0

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

你可以使用以下代码片段来创建圆角:

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

这样,你还可以独立指定每个角的半径。

英文:

You can go for creating a drawable and then making it the background of the view.

You can use the following piece of code to make round corners- just create a new drawable and

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;shape xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; &gt;
    &lt;stroke
        android:width=&quot;1dp&quot;
        android:color=&quot;@color/colorPrimaryDark&quot; /&gt;
    &lt;solid android:color=&quot;#2ED771&quot; /&gt;
    &lt;padding
        android:left=&quot;1dp&quot;
        android:right=&quot;1dp&quot;/&gt;
    &lt;corners android:radius=&quot;15dp&quot; /&gt;
&lt;/shape&gt;

WIth this you can also specify radius of each corner independently

huangapple
  • 本文由 发表于 2020年8月14日 00:13:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63399148.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定