边距不适用于滚动视图

Margins not working on a scrollview

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 可见性设置为消失,当用户添加图像时,可见性变为可见并且 imageview 向下推动提交按钮。

然而,结果并不理想,它使提交按钮锚定到布局的最底部。正如您在下面看到的:

我的问题是,如何使此布局符合提交按钮的底部边距?

layout_marginBottom 添加到其父项(在本例中为 ConstraintLayout)可能有效。

在我看来,所有这些约束属性都可能弄乱了边距。对于此布局,我认为您不需要 ConstraintLayoutLinearLayout 就可以了。

我也遇到了同样的问题,通过用另一个没有边距的 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>

margin 问题只发生在 scrollview 的根子元素上,您可以将边距添加到内部子元素。

android:paddingBottom 添加到父 ConstraintLayout 有效。