Android 预览中未显示 CardView 圆角

CardView rounded corners not showing up in Android preview

我的问题

我通过 XML 定义的卡片视图 在 Android 工作室预览 中未显示其正确的圆角(它们被其子视图覆盖)。

虽然在模拟器中渲染后显示正常,但我担心这个"glitch"可能是我做错了什么或忘记了一些关键属性的症状。即使这不是更深层次问题的征兆,任何人都可以就如何解决这个美学错误提供任何见解吗?

有人能发表意见吗?

<android.support.v7.widget.CardView
    android:layout_marginTop="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="16dp"

    app:cardCornerRadius="10dp"

    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.AppCompatImageView
        android:src="@color/red"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</android.support.v7.widget.CardView>

Android 工作室截图

Android 模拟器截图

此处圆角与您的代码一起正常显示。我的测试 phone 是 Android 7.1。所以我认为您应该清除 phone 的缓存并重新安装此应用程序。或者你应该增加

的值

app:cardCornerRadius

然后再测试一下。

生成的预览中的问题是 CardView 中的内容未 剪切 CardView 的边界,所以角落是你看到的是 ImageView 的角,当您在 emulator/device 上 运行 时,它实际上被剪掉了。

如果你真的想生成 real-like 预览,你可以试试这个解决方法。

  1. 添加一个具有所需角半径的存根可绘制对象。

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" 
      android:shape="rectangle">
    
      <corners android:radius="20dp" />
      <solid android:color="@android:color/holo_green_light" />
    
    </shape>
    
  2. 然后设置为tools:命名空间下ImageView的背景。

    <ImageView android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:src="@android:color/holo_blue_bright"
      tools:src="@drawable/test" />
    

Android Studio 支持 tools: namespace 中的各种 XML 属性,这些属性启用 design-time 功能(例如 [=14= 的 src ] 在预览模式下显示)。