Android CardView 布局损坏 API 28
Android CardView layout broken pre-API 28
我有使用 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'
依赖项。
问题似乎出在 cardCornerRadius
的值设置上。将它设置为 20dp 让我得到了想要的 border-radius 而不会破坏布局。
您可以使用 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"
>
导致不一致。不同的布局最终 hard-coding 不同的字体大小和边距,使应用程序看起来不够精致。
尝试
<?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 replaces the original support library APIs with packages in
the androidx namespace. Only the package and Maven artifact names
changed; class, method, and field names did not change.
喜欢
<androidx.cardview.widget.CardView
我有使用 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'
依赖项。
问题似乎出在 cardCornerRadius
的值设置上。将它设置为 20dp 让我得到了想要的 border-radius 而不会破坏布局。
您可以使用 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"
>
导致不一致。不同的布局最终 hard-coding 不同的字体大小和边距,使应用程序看起来不够精致。
尝试
<?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 replaces the original support library APIs with packages in the androidx namespace. Only the package and Maven artifact names changed; class, method, and field names did not change.
喜欢
<androidx.cardview.widget.CardView