【问题标题】:Android CardView layout broken pre-API 28Android CardView 布局损坏 pre-API 28
【发布时间】:2020-10-05 03:20:35
【问题描述】:

我有一个使用CardView 的视图布局,它应该看起来像这样

在运行 Android 9 及更高版本 (API 28+) 的设备上看起来不错。然而,在任何较旧的东西上,它看起来都被打破了:

布局如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="125dp"
    android:layout_height="40dp"
    android:layout_marginEnd="8dp"
    app:cardCornerRadius="165dp"
    app:cardElevation="8dp"
    app:contentPadding="8dp"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        >

        <android.support.v7.widget.CardView
            android:layout_width="24dp"
            android:layout_height="24dp"
            app:cardCornerRadius="16dp"
            app:cardElevation="0dp"
            >

            <ImageView
                android:id="@+id/tab_icon"
                android:layout_width="26dp"
                android:layout_height="26dp"
                android:layout_gravity="center"
                tools:src="@tools:sample/avatars"
                />

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

        <TextView
            android:id="@+id/tab_title"
            android:layout_width="60dp"
            android:layout_height="match_parent"
            android:layout_marginStart="4dp"
            android:layout_marginEnd="6dp"
            android:ellipsize="end"
            android:gravity="center_vertical"
            android:maxLines="1"
            android:paddingBottom="2dp"
            android:textColor="@android:color/black"
            android:textSize="14sp"
            tools:text="Sample User"
            />

        <ImageView
            android:id="@+id/tab_close"
            android:layout_width="14dp"
            android:layout_height="14dp"
            android:src="@drawable/btn_close"
            />

    </LinearLayout>

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

我正在使用 'com.android.support:cardview-v7:28.0.0' 依赖项。

【问题讨论】:

    标签: android material-design android-cardview


    【解决方案1】:

    似乎问题在于为cardCornerRadius 设置的值。将其设置为 20dp 可以让我在不破坏布局的情况下获得所需的边框半径。

    【讨论】:

      【解决方案2】:

      您可以使用SDP - 可缩放的大小单位

      dependencies {
        implementation 'com.intuit.sdp:sdp-android:1.0.6'
      }
      

      仅供参考

       <android.support.v7.widget.CardView 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="125dp"
        android:layout_height="40dp"
        android:layout_marginEnd="8dp"
        app:cardCornerRadius="165dp"
        app:cardElevation="8dp"
        app:contentPadding="8dp"
        >
      

      导致不一致。不同的布局最终会硬编码不同的字体大小和边距,使应用看起来不那么精致。

      试试

      <?xml version="1.0" encoding="utf-8"?>
         <android.support.v7.widget.CardView 
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="@dimen/_125sdp"
          android:layout_height="@dimen/_35sdp"
          android:layout_marginEnd="@dimen/_5sdp"
          app:cardCornerRadius="@dimen/_150sdp"
          app:cardElevation="@dimen/_5sdp"
          app:contentPadding="@dimen/_5sdp"
          >
           .........
          <android.support.v7.widget.CardView
                      android:layout_width="@dimen/_22sdp"
                      android:layout_height="@dimen/_22sdp"
                      app:cardCornerRadius="@dimen/_12sdp"
                      app:cardElevation="0dp"
                      >
      

      注意

      切换到AndroidX

      AndroidX 将原来的支持库 API 替换为 androidx 命名空间。只有包和 Maven 工件名称 改变了;类、方法和字段名称没有改变。

      喜欢

      <androidx.cardview.widget.CardView
      

      【讨论】:

        猜你喜欢
        • 2015-10-16
        • 1970-01-01
        • 1970-01-01
        • 2015-11-29
        • 1970-01-01
        • 2014-06-17
        • 2021-12-30
        • 2015-11-23
        • 2015-06-29
        相关资源
        最近更新 更多