简体   繁体   中英

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:

image of problem

and this is my layout xml code:

<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. 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. (See " Chain Style " .)

Change the chain style to packed and add a top margin of 8dp to TV_max_angle .

<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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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