简体   繁体   中英

android constraint layout alignment between textview and edittext

I need to put a TextView and EditText in the same line within constraint layout and keep it align each other: below code generate a picture like below:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/serverIpAddr"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="192.168.1.11"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/serverPort"
        style="@style/Widget.AppCompat.EditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="0dp"
        android:ems="10"
        android:inputType="number"
        android:text="8080"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        app:layout_constraintStart_toEndOf="@+id/serverIpAddr"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Now it looks ugly, the TextView and EditText log align horizontally correctly, TextView a little bit above EditText

在此处输入图像描述

You can use app:layout_constraintBaseline_toBaselineOf attribute to make the text of both horizontally aligned

<TextView
    android:id="@+id/serverIpAddr"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="192.168.1.11"
    app:layout_constraintBaseline_toBaselineOf="@id/serverPort"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

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