【问题标题】:CardView Padding/Spacing Varies on Different API LevelCardView 填充/间距在不同的 API 级别上有所不同
【发布时间】:2016-10-31 13:25:43
【问题描述】:

我正在多个设备上测试我的应用程序,并注意到使用 CardView,某些设备会在每个 CardView 之间呈现间距。

我正在尝试隔离问题,看起来 API 23 不包含任何间距,而 API

请注意,我已经尝试过here 提出的解决方案,但没有奏效。它在 XML 中提出了以下内容:

app:cardUseCompatPadding="true"

API 23

API

【问题讨论】:

    标签: java android xml android-studio android-cardview


    【解决方案1】:

    问题在于,API-21 及更高版本原生支持 CardView,而在 KitKat 及更早版本上,它们被支持库“伪造”。

    我过去成功的做法是使用单独的布局文件,然后手动对它们进行像素推送,直到看起来正确为止。

    例如,在res/layout,你有这个xml:

    <android.support.v7.widget.CardView
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:cardview="http://schemas.android.com/apk/res-auto"
         xmlns:tools="http://schemas.android.com/tools"
         android:id="@+id/my_card"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:layout_marginRight="0dip"
         android:layout_marginLeft="0dip"
         android:layout_marginTop="1dip"
         android:layout_marginBottom="1dip"
         cardview:cardBackgroundColor="@color/even_light_gray"
         android:foreground="@drawable/card_foreground"
         cardview:cardCornerRadius="1dp" >
    
         <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingBottom="8dp"
            android:paddingLeft="2dp"
            android:paddingRight="2dp"
            android:paddingTop="4dp" >
    
            <!-- .......... -->
    
            </LinearLayout>
    </android.support.v7.widget.CardView>
    

    然后,在res/layout-21 中,您将拥有这个具有不同边距和填充值的布局:

    <android.support.v7.widget.CardView
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:cardview="http://schemas.android.com/apk/res-auto"
         xmlns:tools="http://schemas.android.com/tools"
         android:id="@+id/my_card"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:layout_marginRight="1dip"
         android:layout_marginLeft="1dip"
         android:layout_marginTop="4dip"
         android:layout_marginBottom="4dip"
         cardview:cardBackgroundColor="@color/even_light_gray"
         android:foreground="@drawable/card_foreground"
         cardview:cardCornerRadius="1dp" >
    
         <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingBottom="8dp"
            android:paddingLeft="4dp"
            android:paddingRight="4dp"
            android:paddingTop="4dp" >
    
            <!-- .......... -->
    
            </LinearLayout>
    </android.support.v7.widget.CardView>
    

    【讨论】:

    • 这个 XML 是否允许在 CardView 上没有填充?最终,我正在尝试删除所有 API 级别的填充...
    • 看起来你可能根本不想使用 CardView。通常,当您需要拐角半径和高度时,您会使用 CardView,而您甚至都没有使用。尝试只使用 LinearLayout 或 RelativeLayout 而不是 CardView。
    【解决方案2】:

    我发现这个... 在cardview attr中使用这个,
    app:cardPreventCornerOverlap="false"
    并在不同的 android 操作系统版本中使用它
    app:cardMaxElevation="0dp" app:cardElevation="0dp"
    在 pre-L 中,将它们设为 0dp,在 api21 或之后,将它们设为非零

    【讨论】:

      猜你喜欢
      • 2014-01-03
      • 1970-01-01
      • 2018-08-14
      • 2011-06-30
      • 2016-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多