Android自定义对话框圆角。

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

Android Custom Dialog Corner Radius

问题

我正在在我的Android应用中填充自定义对话框。对话框的布局如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content"
  6. android:id="@+id/parent"
  7. app:cardBackgroundColor="@android:color/white"
  8. app:cardCornerRadius="15dp"
  9. app:cardElevation="3dp">
  10. <LinearLayout
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_gravity="center_horizontal"
  14. android:id="@+id/interior_layout"
  15. android:orientation="vertical">
  16. <ImageView
  17. android:layout_width="80dp"
  18. android:layout_height="80dp"
  19. android:scaleType="centerCrop"
  20. android:adjustViewBounds="true"
  21. android:layout_gravity="center_horizontal"
  22. android:visibility="gone"
  23. android:id="@+id/imageView"/>
  24. <TextView
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:id="@+id/message"
  28. android:textSize="14sp"
  29. android:textColor="@android:color/black"
  30. android:visibility="gone"
  31. android:layout_marginTop="10dp"
  32. android:text="Hello"
  33. android:layout_gravity="center_horizontal"/>
  34. </LinearLayout>
  35. </androidx.cardview.widget.CardView>

这是我用来填充对话框的代码:

  1. Dialog alertDialog = new Dialog(activity);
  2. alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
  3. dialogView = LayoutInflater.from(activity).inflate(R.layout.layout_progress_dialog, null);
  4. alertDialog.setContentView(dialogView);
  5. 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:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;androidx.cardview.widget.CardView xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. android:layout_width=&quot;wrap_content&quot;
  5. android:layout_height=&quot;wrap_content&quot;
  6. android:id=&quot;@+id/parent&quot;
  7. app:cardBackgroundColor=&quot;@android:color/white&quot;
  8. app:cardCornerRadius=&quot;15dp&quot;
  9. app:cardElevation=&quot;3dp&quot;&gt;
  10. &lt;LinearLayout
  11. android:layout_width=&quot;wrap_content&quot;
  12. android:layout_height=&quot;wrap_content&quot;
  13. android:layout_gravity=&quot;center_horizontal&quot;
  14. android:id=&quot;@+id/interior_layout&quot;
  15. android:orientation=&quot;vertical&quot;&gt;
  16. &lt;ImageView
  17. android:layout_width=&quot;80dp&quot;
  18. android:layout_height=&quot;80dp&quot;
  19. android:scaleType=&quot;centerCrop&quot;
  20. android:adjustViewBounds=&quot;true&quot;
  21. android:layout_gravity=&quot;center_horizontal&quot;
  22. android:visibility=&quot;gone&quot;
  23. android:id=&quot;@+id/imageView&quot;/&gt;
  24. &lt;TextView
  25. android:layout_width=&quot;wrap_content&quot;
  26. android:layout_height=&quot;wrap_content&quot;
  27. android:id=&quot;@+id/message&quot;
  28. android:textSize=&quot;14sp&quot;
  29. android:textColor=&quot;@android:color/black&quot;
  30. android:visibility=&quot;gone&quot;
  31. android:layout_marginTop=&quot;10dp&quot;
  32. android:text=&quot;Hello&quot;
  33. android:layout_gravity=&quot;center_horizontal&quot;/&gt;
  34. &lt;/LinearLayout&gt;
  35. &lt;/androidx.cardview.widget.CardView&gt;

This is my code to inflate the dialog:

  1. Dialog alertDialog = new Dialog(activity);
  2. alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
  3. dialogView = LayoutInflater.from(activity).inflate(R.layout.layout_progress_dialog, null);
  4. alertDialog.setContentView(dialogView);
  5. 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**方法:

  1. class RoundedDialog: DialogFragment() {
  2. override fun getTheme() = R.style.RoundedCornersDialog
  3. override fun onCreateView(...): View? {
  4. val v: View = inflater.inflate(R.layout...., container, false)
  5. return v
  6. }
  7. //...
  8. }

与以下样式一起使用:

  1. <style name="RoundedCornersDialog" parent="@style/Theme.MaterialComponents.Dialog">
  2. <item name="dialogCornerRadius">16dp</item>
  3. </style>

如果你想使用**AlertDialog**,可以在onCreateDialog中创建它。查看这个答案以获取更多详细信息

英文:

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

Just use the getTheme method:

  1. class RoundedDialog: DialogFragment() {
  2. override fun getTheme() = R.style.RoundedCornersDialog
  3. override fun onCreateView(...): View? {
  4. val v: View = inflater.inflate(R.layout...., container, false)
  5. return v
  6. }
  7. //...
  8. }

with:

  1. &lt;style name=&quot;RoundedCornersDialog&quot; parent=&quot;@style/Theme.MaterialComponents.Dialog&quot;&gt;
  2. &lt;item name=&quot;dialogCornerRadius&quot;&gt;16dp&lt;/item&gt;
  3. &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

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

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">
  3. <stroke
  4. android:width="1dp"
  5. android:color="@color/colorPrimaryDark" />
  6. <solid android:color="#2ED771" />
  7. <padding
  8. android:left="1dp"
  9. android:right="1dp" />
  10. <corners android:radius="15dp" />
  11. </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

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;shape xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; &gt;
  3. &lt;stroke
  4. android:width=&quot;1dp&quot;
  5. android:color=&quot;@color/colorPrimaryDark&quot; /&gt;
  6. &lt;solid android:color=&quot;#2ED771&quot; /&gt;
  7. &lt;padding
  8. android:left=&quot;1dp&quot;
  9. android:right=&quot;1dp&quot;/&gt;
  10. &lt;corners android:radius=&quot;15dp&quot; /&gt;
  11. &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:

确定