没有 paddingTop,Android paddingBottom 无法工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12471688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:45:25  来源:igfitidea点击:

Android paddingBottom not working without paddingTop

androidlayoutscrollviewpadding

提问by Aelexe

I am currently using a layout that contains a RelativeLayout inside a ScrollView.

我目前使用的布局在 ScrollView 中包含一个 RelativeLayout。

I want the RelativeLayout to be contained 5dp away from the bottom of the ScrollView so it does not overlap the background I have behind it, to achieve this I was using this XML:

我希望将 RelativeLayout 包含在距离 ScrollView 底部 5dp 的地方,这样它就不会与我背后的背景重叠,为此我使用了这个 XML:

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/backgroundView1"
    android:fadingEdge="none"
    android:scrollbars="none"
    android:paddingTop="5dp"
    android:paddingBottom="5dp">

    <RelativeLayout
        android:id="@+id/innerLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </RelativeLayout>
</ScrollView>

This worked perfectly, however I no longer need the padding at the top. When removing the paddingTop line, the paddingBottom no longer functions. Even if I set the paddingBottom to 100dp it shows no effect on my layout.

这工作得很好,但是我不再需要顶部的填充。删除 paddingTop 行时, paddingBottom 不再起作用。即使我将 paddingBottom 设置为 100dp,它也不会影响我的布局。

I tried paddingTop="0dp" and that did not fix the issue either, it seems paddingBottom will only work when paddingTop is above 0.

我尝试了 paddingTop="0dp" 并且这也没有解决问题,似乎 paddingBottom 仅在 paddingTop 大于 0 时才有效。

Anyone got any ideas as to why paddingBottom does not work without paddingTop?

任何人都知道为什么 paddingBottom 在没有 paddingTop 的情况下不起作用?

回答by Hiren Patel

You wrote xml property android:layout_alignParentBottom="true"so your ScrollViewwill always it will be aligned bottom.

您编写了 xml 属性android:layout_alignParentBottom="true"因此您的ScrollView将始终对齐底部

Remove android:layout_alignParentBottom="true"and try again.

删除android:layout_alignParentBottom="true" 并重试。

回答by amit

use this kind of layout for scrollview and change according to your need..dont use relative layout inside scrollview. Here you can set any type height in image and still you will see padding at bottom without any paddingtop

将这种布局用于滚动视图并根据您的需要进行更改..不要在滚动视图中使用相对布局。在这里,您可以在图像中设置任何类型的高度,但您仍然会在底部看到填充而没有任何填充顶部

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/backgroundView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#cccccc"
        android:paddingBottom="5dp"
        android:scrollbars="none" >

        <LinearLayout
            android:id="@+id/innerLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="700dp"
                android:background="@android:color/transparent"
                android:scaleType="fitXY"
                android:src="@drawable/image2" />
        </LinearLayout>
    </ScrollView>

</LinearLayout>

回答by Mohamed Adel

you can use

您可以使用

    android:layout_marginTop="10dip"

or

或者

    android:layout_marginBottom="20dip"

回答by Kurdish Programmer

This is for having (5 dp) space on top and bottom

这是为了在顶部和底部有 (5 dp) 空间

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/backgroundView1"
    android:fadingEdge="none"
    android:scrollbars="none"
    android:paddingTop="5dp"
    android:paddingBottom="5dp">

    <RelativeLayout
        android:id="@+id/innerLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp">
    </RelativeLayout>
</ScrollView>

This is for having (5 dp) space on top, bottom, left, and right

这是为了在顶部、底部、左侧和右侧有 (5 dp) 空间

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/backgroundView1"
    android:fadingEdge="none"
    android:scrollbars="none"
    android:paddingTop="5dp"
    android:paddingBottom="5dp">

    <RelativeLayout
        android:id="@+id/innerLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp">
    </RelativeLayout>
</ScrollView>