简体   繁体   English

Android布局不需要的填充

[英]Android layout unwanted padding

So i am having this layout file (below). 所以我有这个布局文件(下面)。 As you can see, there is no padding or margin. 如您所见,没有填充或边距。 The dimen.xml files also dont have any padding/margin. dimen.xml文件也没有任何填充/边距。 Finally, i do NOT change the layout programmatically at all. 最后,我根本不以编程方式更改布局。

      <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>

Below are two screenshots from two different devices. 以下是来自两个不同设备的两个屏幕截图。

Lenovo k3 Note: 联想k3注:

在此输入图像描述

HTC Desire 550: HTC Desire 550:

在此输入图像描述

Any ideas what might be the cause of this? 任何想法可能是什么原因?

Ok Guys... thanks for your input. 好的伙计们...感谢您的投入。 Turns out the "Cardview Documentation" mentions that: 原来“Cardview文档”提到:

Before L, CardView adds padding to its content and draws shadows to that area. 在L之前,CardView会在其内容中添加填充并为该区域绘制阴影。 This padding amount is equal to maxCardElevation + (1 - cos45) * cornerRadius on the sides and maxCardElevation * 1.5 + (1 - cos45) * cornerRadius on top and bottom. 此填充量等于两侧的maxCardElevation +(1 - cos45)* cornerRadius,顶部和底部的maxCardElevation * 1.5 +(1 - cos45)* cornerRadius。

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: 对于每个遇到相同问题的人来说,一种可能的解决方案是在布局中使用属性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>

This will cause your layout to be "rendered" the same way (the pre-Lollipop way //sad ) in every device regardless of the API. 这将导致您的布局在每个设备中以相同的方式(前Lollipop方式//悲伤)“呈现”,无论API如何。 At least this way we can fix a common layout that works for all versions given this restriction. 至少通过这种方式,我们可以修复一个适用于所有版本的公共布局。

Hope it helps. 希望能帮助到你。

UPDATE: If you decide to go for "app:cardUseCompatPadding=true" here is another advice. 更新:如果您决定选择“app:cardUseCompatPadding = true”,这是另一个建议。 Use negative layout_margin, to balance-out the unwanted padding from the compat library. 使用负布局_margin来平衡compat库中不需要的填充。 Example below: 示例如下:

<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>

This way your layout can get rid of unwanted padding AND look the same pre-lollipop and after lollipop. 通过这种方式,您的布局可以摆脱不必要的填充并看起来相同的前棒棒糖和棒棒糖之后。

Hope this helps even more. 希望这会有所帮助。 :) :)

The possible reason is that well HTC mobiles especially have different types of internal layout files. 可能的原因是HTC移动设备特别具有不同类型的内部布局文件。 So Lenovo Nexus and SAMSUNG may be functioning properly but thanks to HTC's difference in these situations it is a big problem for lot of developers. 因此,联想Nexus和SAMSUNG可能正常运行,但由于HTC在这些情况下的不同,这对很多开发人员来说都是一个大问题。 If you really want it to work on HTC as well I recommend the best way to make it work on HTC is to create your own xml files which resemble the android support ones. 如果你真的希望它能够在HTC上工作,我建议让它在HTC上工作的最好方法是创建你自己的xml文件,类似于android支持的文件。 I recommend if you want to proceed you try to get a hang of the native xml file of the android card one and use the same. 我建议如果你想继续尝试获取android卡的本机xml文件的挂起并使用相同的。 Now HTC has to use your custom ones which are equal to all other android ones but not in HTC. 现在HTC必须使用你的自定义的那些与所有其他Android机器人相同但不在HTC中。 What I am saying is that now It will work properly on all devices. 我所说的是现在它可以在所有设备上正常工作。 But be warned, this just might turn out to be long and painful if you can't get a hang of the native xml file. 但要注意,如果您无法获取原生xml文件,这可能会变得漫长而痛苦。 If you do it'll probably then become a breeze for you. 如果你这样做,那么对你来说可能会变得轻而易举。 I hope you understand me. 我希望你能理解我。

Have you tried setting the height for your linear layout or set it to match parent instead? 您是否尝试为线性布局设置高度或将其设置为与父级相匹配? It doesn't seem like there is extra padding but rather that your items in the list are not the same height as the image even gets Cut off. 它似乎没有额外的填充,而是列表中的项目与图像甚至被切断的高度不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM