滚动视图上的边距无效。

3

XML:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fancy="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBlack"
android:fillViewport="true"
android:orientation="vertical">

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:background="@color/colorNavigationKolBG"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:orientation="vertical">

  <!-- FORM STUFF HERE, I CUT IT OUT -->

    <mehdi.sakout.fancybuttons.FancyButton
        android:id="@+id/btn_add_pic"
        android:layout_width="250dp"
        android:layout_height="40dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="7dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/descriptionForm"
        fancy:fb_borderColor="@color/colorNavigationKolBTN"
        fancy:fb_borderWidth="1dp"
        fancy:fb_defaultColor="@color/colorNavigationKolBTN"
        fancy:fb_focusColor="#3B5F6A"
        fancy:fb_fontIconResource="&#xf030;"
        fancy:fb_fontIconSize="20sp"
        fancy:fb_iconPosition="left"
        fancy:fb_radius="0dp"
        fancy:fb_text="Add Image"
        fancy:fb_textColor="#FFFFFF"
        fancy:fb_textSize="20sp" />

    <mehdi.sakout.fancybuttons.FancyButton
        android:id="@+id/btn_submit"
        android:layout_width="250dp"
        android:layout_height="60dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearImageLayout"
        fancy:fb_borderColor="@color/colorNavigationKolBTN"
        fancy:fb_borderWidth="1dp"
        fancy:fb_defaultColor="@color/colorNavigationKolBTN"
        fancy:fb_focusColor="#3B5F6A"
        fancy:fb_fontIconResource="&#xf00c;"
        fancy:fb_fontIconSize="20sp"
        fancy:fb_iconPosition="left"
        fancy:fb_radius="0dp"
        fancy:fb_text="Submit"
        fancy:fb_textColor="#FFFFFF"
        fancy:fb_textSize="20sp" />


    <LinearLayout
        android:id="@+id/linearImageLayout"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/border"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn_add_pic"
        fancy:srcCompat="@color/cardview_light_background">

        <ImageView
            android:id="@+id/imageForm"
            android:layout_width="match_parent"
            android:layout_height="150dp" />

    </LinearLayout>


</android.support.constraint.ConstraintLayout>

ImageView的可见性被设置为gone,当用户添加图像时,可见性会更改为visible,然后ImageView会将提交按钮向下推。然而,结果并不理想,它使提交按钮停留在布局的最底部。如下图所示:
enter image description here
我的问题是,我该怎么做才能使布局尊重提交按钮的底部边距?
3个回答

2
android:paddingBottom添加到父ConstraintLayout中即可。

1
layout_marginBottom添加到其父级(在本例中为ConstraintLayout)可能有效。
对我来说,所有这些约束属性似乎可能会搞乱边距。对于此布局,我认为您不需要ConstraintLayout。使用LinearLayout即可。

我切换到线性布局,但无论是约束布局还是线性布局都无法解决这个问题。我已经在线性布局和约束布局中都添加了layout_marginBottom。 - Rosenberg
你的按钮的 layout_marginBottom 属性正在起作用。按钮下方的黑色空间是一个边距(请注意,你的滚动视图具有黑色背景)。你在这里遇到的问题可能是你的 ConstraintLayout 的高度。它的高度为 0dp,并将变成其子元素的高度。也许如果你将其设置为 match_parent,你会得到想要的绿色背景。 - Gabriel Costa

0

我也遇到了同样的问题,解决方法是将子布局包裹在另一个没有边距的LinearLayout中。在你的情况下,应该像这样:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fancy="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBlack"
android:fillViewport="true"
android:orientation="vertical">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:background="@color/colorNavigationKolBG"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:orientation="vertical">

  <!-- FORM STUFF HERE, I CUT IT OUT -->

    <mehdi.sakout.fancybuttons.FancyButton
        android:id="@+id/btn_add_pic"
        android:layout_width="250dp"
        android:layout_height="40dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="7dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/descriptionForm"
        fancy:fb_borderColor="@color/colorNavigationKolBTN"
        fancy:fb_borderWidth="1dp"
        fancy:fb_defaultColor="@color/colorNavigationKolBTN"
        fancy:fb_focusColor="#3B5F6A"
        fancy:fb_fontIconResource="&#xf030;"
        fancy:fb_fontIconSize="20sp"
        fancy:fb_iconPosition="left"
        fancy:fb_radius="0dp"
        fancy:fb_text="Add Image"
        fancy:fb_textColor="#FFFFFF"
        fancy:fb_textSize="20sp" />

    <mehdi.sakout.fancybuttons.FancyButton
        android:id="@+id/btn_submit"
        android:layout_width="250dp"
        android:layout_height="60dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearImageLayout"
        fancy:fb_borderColor="@color/colorNavigationKolBTN"
        fancy:fb_borderWidth="1dp"
        fancy:fb_defaultColor="@color/colorNavigationKolBTN"
        fancy:fb_focusColor="#3B5F6A"
        fancy:fb_fontIconResource="&#xf00c;"
        fancy:fb_fontIconSize="20sp"
        fancy:fb_iconPosition="left"
        fancy:fb_radius="0dp"
        fancy:fb_text="Submit"
        fancy:fb_textColor="#FFFFFF"
        fancy:fb_textSize="20sp" />


    <LinearLayout
        android:id="@+id/linearImageLayout"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/border"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn_add_pic"
        fancy:srcCompat="@color/cardview_light_background">

        <ImageView
            android:id="@+id/imageForm"
            android:layout_width="match_parent"
            android:layout_height="150dp" />

    </LinearLayout>


</android.support.constraint.ConstraintLayout>

    </LinearLayout>

</ScrollView>

边距问题仅会影响滚动视图的根子级,您可以将边距添加到内部子级。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接