英文:
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:
<?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>
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:
<style name="RoundedCornersDialog" parent="@style/Theme.MaterialComponents.Dialog">
<item name="dialogCornerRadius">16dp</item>
</style>
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
<?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>
WIth this you can also specify radius of each corner independently
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论