简体   繁体   English

约束布局意外边距

[英]constraint layout unexpected margin

i'm trying to implement a layout ui using constraint layout and everything seems fine except that i have a big unexpected top margin:我正在尝试使用约束布局来实现布局 ui,一切似乎都很好,除了我有一个意想不到的上边距:

image of problem问题图片

and this is my layout xml code:这是我的布局 xml 代码:

<android.support.constraint.ConstraintLayout
                android:id="@+id/relativeLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="40dp"
                android:layout_marginEnd="40dp"
                android:layout_marginBottom="15dp">

                <Button
                    android:id="@+id/btn_reset"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/custom_ripple"
                    android:enabled="false"
                    android:text="@string/reset"
                    android:textAllCaps="false"
                    android:textColor="@color/white"
                    app:layout_constraintBottom_toTopOf="@+id/view6"
                    app:layout_constraintEnd_toStartOf="@+id/TV_current_angle"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <Button
                    android:id="@+id/btn_stop"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/custom_ripple"
                    android:enabled="false"
                    android:text="@string/stop"
                    android:textAllCaps="false"
                    android:textColor="@color/white"
                    app:layout_constraintBottom_toTopOf="@+id/view6"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toEndOf="@+id/TV_current_angle"
                    app:layout_constraintTop_toTopOf="parent" />

                <TextView
                    android:id="@+id/TV_max_angle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="32dp"
                    android:text="@string/maximum_of_angle"
                    android:textAlignment="center"
                    android:textColor="@color/colorPrimaryDark2"
                    android:textSize="15sp"
                    android:textStyle="bold"
                    app:layout_constraintBottom_toTopOf="@+id/TV_current_angle"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintVertical_bias="0.0" />

                <TextView
                    android:id="@+id/TV_current_angle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/angle"
                    android:textAlignment="center"
                    android:textColor="@color/colorPrimaryDark2"
                    android:textSize="15sp"
                    android:textStyle="bold"
                    app:layout_constraintBottom_toTopOf="@+id/TV_min_angle"
                    app:layout_constraintEnd_toStartOf="@+id/btn_stop"
                    app:layout_constraintStart_toEndOf="@+id/btn_reset"
                    app:layout_constraintTop_toBottomOf="@+id/TV_max_angle" />

                <TextView
                    android:id="@+id/TV_min_angle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="32dp"
                    android:text="@string/minimum_of_angle"
                    android:textAlignment="center"
                    android:textColor="@color/colorPrimaryDark2"
                    android:textSize="15sp"
                    android:textStyle="bold"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/TV_current_angle" />

                <View
                    android:id="@+id/view5"
                    android:layout_width="250dp"
                    android:layout_height="1dp"
                    android:layout_marginTop="16dp"
                    android:background="@color/colorPrimaryDark2"
                    app:layout_constraintBottom_toTopOf="@+id/btn_start"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/TV_min_angle" />

                <Button
                    android:id="@+id/btn_start"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="8dp"
                    android:layout_marginBottom="8dp"
                    android:background="@drawable/custom_ripple"
                    android:text="@string/start"
                    android:textAllCaps="false"
                    android:textColor="@color/white"
                    app:layout_constraintBottom_toTopOf="@+id/view6"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/view5"
                    tools:ignore="RelativeOverlap" />

                <View
                    android:id="@+id/view6"
                    android:layout_width="300dp"
                    android:layout_height="1dp"
                    android:layout_marginBottom="15dp"
                    android:background="@color/colorPrimaryDark2"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/btn_start" />

            </android.support.constraint.ConstraintLayout>

there is no padding and no unwanted margin in the code (or maybe i missed) and i checked it many times to find the cause of this problem but could not find it so decided to ask about that here.代码中没有填充也没有多余的边距(或者我可能错过了),我检查了很多次以找到这个问题的原因,但找不到它所以决定在这里询问。

I want to get rid of that top margin or at most have an 8dp margin.我想摆脱上边距或最多有 8dp 边距。 any help will be appreciated.任何帮助将不胜感激。

Your TextViews are placed inside a chain that is anchored to the parent's top and bottom with a default chain style of spread which is the default.您的TextViews放置在一个链中,该链锚定到父级的顶部和底部,默认链spread为默认值。 (See " Chain Style " .) (参见链式 。)

Change the chain style to packed and add a top margin of 8dp to TV_max_angle .将链样式更改为packed并为8dp添加8dp的上边距。

<TextView
    android:id="@+id/TV_max_angle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="32dp"
    android:text="@string/maximum_of_angle"
    android:textAlignment="center"
    android:textColor="@color/colorPrimaryDark2"
    android:textSize="15sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toTopOf="@+id/TV_current_angle"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0"
    app:layout_constraintVertical_chainStyle="packed" />

This produces the following layout:这会产生以下布局:

在此处输入图像描述

You may need to adjust the location of the buttons, but this will eliminate the excess space at the top.您可能需要调整按钮的位置,但这将消除顶部的多余空间。

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

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