边距不在滚动视图上

时间:2017-06-06 14:38:04

标签: android xml android-constraintlayout

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>

图像视图可见性设置为消失,当用户添加图像时,可见性更改为可见,并且图像视图向下推动提交按钮。

但是,结果是不合需要的,它使提交按钮锚定到布局的最底部。如下所示: enter image description here

我的问题是,如何让这个布局尊重提交按钮的下边距?

3 个答案:

答案 0 :(得分:1)

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

在我看来,所有这些约束属性可能都搞砸了边缘。对于这种布局,我认为您不需要ConstraintLayoutLinearLayout可以做得很好。

答案 1 :(得分: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>

保证金问题仅导致scrollview的根子项,您可以将边距添加到内部子项。

答案 2 :(得分:0)

android:paddingBottom添加到父ConstraintLayout即可。