所以我有这个布局文件(如下)。正如您所看到的,没有填充或边距。 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>
以下是来自两个不同设备的两个屏幕截图。
联想k3注意:
HTC 渴望 550:
有什么想法可能是什么原因造成的吗?
好的,伙计们...感谢您的投入。事实证明“卡片视图文档” https://developer.android.com/reference/android/support/v7/widget/CardView.html提到:
在 L 之前,CardView 向其内容添加填充并在该区域绘制阴影。此填充量等于侧面的 maxCardElevation + (1 - cos45) *cornerRadius 以及顶部和底部的 maxCardElevation * 1.5 + (1 - cos45) *cornerRadius。
这意味着边距是绘制阴影和模拟高程的预期行为。对于遇到相同问题的每个人来说,一种可能的解决方案是在布局中使用属性 cardUseCompatPadding,如下所示:
<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>
这将导致您的布局在每个设备中以相同的方式“渲染”(棒棒糖之前的方式 //sad ),无论 API 如何。至少通过这种方式,我们可以修复一个适用于所有版本的通用布局。
希望能帮助到你。
UPDATE:如果您决定选择“app:cardUseCompatPadding=true”,这里有另一个建议。使用负的layout_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>
这样,您的布局就可以摆脱不需要的填充,并且在棒棒糖之前和之后看起来相同。
希望这有更多帮助。 :)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)