问题描述
我使用的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即可获得所需的边框半径,而不会破坏布局。
您可以使用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