简体   繁体   中英

Align TextView correctly with a Spinner in a ConstraintLayout?

I have a activity layout which represents a form. The form contains a few form fields including an Spinner.

The layout is of type ConstraintLayout.

在此输入图像描述

Because the Spinner control doesn't have a baseline i can't vertical align my "Kind" label correctly. I used 24 dp at the top as a temp. solution, but because the Spinner will increase in height i don't like this solution.

Is there a better solution to get the Kind label aligned correctly?

<TextView android:id="@+id/video"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Lorem Ipsum"
          app:layout_constraintBottom_toBottomOf="@+id/spinner"
          app:layout_constraintLeft_toLeftOf="parent"
          app:layout_constraintRight_toRightOf="parent"
          app:layout_constraintTop_toTopOf="@+id/spinner" />

When you anchor the bottom and top of a view to another, it will be centered relatively. This is valid vertically and horizontally.
Hope this helps.

In my experience, the TextView and the Spinner aligned better when using baseline alignment ( layout_constraintBaseline_toBaselineOf ).

<TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Repeat"/>

<Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"   
        app:layout_constraintLeft_toRightOf="@id/textview"
        app:layout_constraintBaseline_toBaselineOf="@id/textview"
        android:layout_marginLeft="50dp"/>

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