ConstraintLayout:如何根据另一个视图垂直居中视图
ConstratintLayout: How to center a view according to another view, vertically
我有以下代码
<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>
结果是这样
我不知道我描述的是否正确,但是,我想根据金额 EditText
垂直居中金额 TextView
。现在,TextView
与 EditText
的顶部对齐。我想做成这样。
我为此添加了 10dp
或 layout_marginTop
,但我认为这不是解决问题的方法。
提前致谢。
试试这个
<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>
输出
我有以下代码
<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>
结果是这样
我不知道我描述的是否正确,但是,我想根据金额 EditText
垂直居中金额 TextView
。现在,TextView
与 EditText
的顶部对齐。我想做成这样。
我为此添加了 10dp
或 layout_marginTop
,但我认为这不是解决问题的方法。
提前致谢。
试试这个
<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>
输出