android - ConstratintLayout : How to center a view according to another view, 垂直

标签 android android-layout android-constraintlayout

我有以下代码

<android.support.constraint.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"
    android:padding="16dp"
    tools:context="gr.teiath.cs.android.tipcalculator.MainActivity">

    <TextView
        android:id="@+id/AmountLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Amount"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
        app:layout_constraintEnd_toStartOf="@id/AmountTV"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/Amount"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:digits="0123456789"
        android:inputType="numberDecimal"
        android:maxLength="10"
        android:maxLines="1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/AmountLabel"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

结果是这样的

enter image description here

我不知道我的描述是否正确,但是,我想根据 Amount EditText 垂直居中 Amount TextView。现在,TextViewEditText 的顶部对齐。我想做这样的。

enter image description here

我为此添加了 layout_marginTop10dp,但我不认为这是这样做的方法。 提前致谢。

最佳答案

试试这个

<android.support.constraint.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"
    android:padding="16dp">


    <TextView
        android:id="@+id/AmountLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Amount"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBaseline_toBaselineOf="@id/Amount"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/Amount"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:digits="0123456789"
        android:inputType="numberDecimal"
        android:maxLength="10"
        android:maxLines="1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/AmountLabel"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

输出

enter image description here

关于android - ConstratintLayout : How to center a view according to another view, 垂直,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47696326/

相关文章:

android - 如何更改主屏幕小部件中的 TextView 字体样式?

android - 将 View 移出约束布局

android - 如何在android中重新启动时不关闭的警报通知

android - GPU 与 CPU 编程 : inconsistencies in processing times

android - 自定义抽屉导航布局 (xml)

android - 在触摸该列时上下移动 Gridview 的列

android - ConstraintLayout 组与层

android - ConstraintLayout 问题(最大宽度、尺寸比和链样式)

android - 可扩展布局与 recyclerview android

android - 在maven中使用pom.xml执行单元测试后如何停止android模拟器?