Android布局不需要的填充

Dim*_*CBR 6 layout android margin padding

所以我有这个布局文件(下面).如您所见,没有填充或边距.dimen.xml文件也没有任何填充/边距.最后,我根本不以编程方式更改布局.

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/root"
            android:padding="0dp"
            android:orientation="vertical">

            <android.support.v7.widget.CardView
                android:id="@+id/toolbarholder"
                android:layout_alignParentTop="true"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:cardElevation="16dp"
                app:cardCornerRadius="0dp">
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">



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


            <ListView
                android:layout_below="@+id/toolbarholder"
                android:layout_above="@+id/cardroot"
                android:id="@+id/listView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:divider="@null"
                android:dividerHeight="0dp"
                android:layout_marginTop="0dp" />


            <android.support.v7.widget.CardView
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/cardroot"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:background="@color/transparentblack"
                app:cardCornerRadius="0dp"
                app:cardElevation="16dp"
                android:layout_alignParentBottom="true">

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

                    <LinearLayout
                        android:id="@+id/buttonholder"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="20dp"
                        android:weightSum="3">

                        <ImageView
                            android:id="@+id/previous"
                            android:layout_width="35dp"
                            android:layout_height="35dp"
                            android:src="@drawable/previous"
                            android:layout_weight="1"/>
                        <ImageView
                            android:id="@+id/play"
                            android:layout_width="35dp"
                            android:layout_height="35dp"
                            android:src="@drawable/play"
                            android:layout_weight="1"/>
                        <ImageView
                            android:id="@+id/next"
                            android:layout_width="35dp"
                            android:layout_height="35dp"
                            android:src="@drawable/next"
                            android:layout_weight="1"/>

                    </LinearLayout>

                    <SeekBar
                        android:id="@+id/seekbar"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/buttonholder" />

                </RelativeLayout>

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

        </RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

以下是来自两个不同设备的两个屏幕截图.

联想k3注:

在此输入图像描述

HTC Desire 550:

在此输入图像描述

任何想法可能是什么原因?

Dim*_*CBR 11

Ok Guys... thanks for your input. Turns out the "Cardview Documentation" mentions that:

Before L, CardView adds padding to its content and draws shadows to that area. This padding amount is equal to maxCardElevation + (1 - cos45)*cornerRadius on the sides and maxCardElevation*1.5 + (1 - cos45)*cornerRadius on top and bottom.

This means that the Margin is intended behavior to draw the shadows and simulate elevation. One possible solution for everyone having the same problem, is use the attribute cardUseCompatPadding in your layout as follows:

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardElevation="0dp"
    app:cardUseCompatPadding="true">
</android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud)

This will cause your layout to be "rendered" the same way (the pre-Lollipop way //sad ) in every device regardless of the API. At least this way we can fix a common layout that works for all versions given this restriction.

Hope it helps.

更新:如果您决定选择"app:cardUseCompatPadding = true",这是另一个建议.使用负布局_margin来平衡compat库中不需要的填充.示例如下:

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="-10dp"
    app:cardCornerRadius="0dp"
    app:cardUseCompatPadding="true"
    app:cardElevation="8dp">
</android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud)

通过这种方式,您的布局可以摆脱不必要的填充并看起来相同的前棒棒糖和棒棒糖之后.

希望这会有所帮助.:)