Android Studio 布局 - 在同一行上彼此相邻的两个文本视图的父级中水平居中
Android Studio Layout - Horizontal centering in parent of two textviews that are next to each other on same line
我有一个关于 Android Studio 中两个文本视图的相对布局和居中的初学者问题。我有两个文本视图在同一行上对齐,右边的文本视图与 "toEndOf" 左边的文本视图对齐。现在我如何在父级中将它们水平居中,以便文本行在屏幕上从左到右居中?见图。
对于给定的布局,"I Am" 很好地水平居中,但我希望两个文本视图一起居中,以便 "I Am Poor" 居中。
我会将它们放在另一个布局中(与水平方向成线性),然后将另一个布局居中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ONE"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TWO"/>
</LinearLayout>
</RelativeLayout>
RelativeLayout
的现代替代品是 ConstraintLayout
。在 ConstraintLayout 中,您可以创建所谓的 链 视图,然后为整个链定义居中行为。
以下是您的操作方式:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="short"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/large"/>
<TextView
android:id="@+id/large"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="very very very long"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@id/small"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
链是通过将两个视图的开始和结束相互连接并连接到父视图而形成的:
android:id="@+id/small"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/large"
android:id="@+id/large"
app:layout_constraintStart_toEndOf="@id/small"
app:layout_constraintEnd_toEndOf="parent"
然后您还应用 packed
样式,以便视图彼此紧挨着定位,而不是在它们之间展开 space:
app:layout_constraintHorizontal_chainStyle="packed"
最后是这样的结果:
我有一个关于 Android Studio 中两个文本视图的相对布局和居中的初学者问题。我有两个文本视图在同一行上对齐,右边的文本视图与 "toEndOf" 左边的文本视图对齐。现在我如何在父级中将它们水平居中,以便文本行在屏幕上从左到右居中?见图。
对于给定的布局,"I Am" 很好地水平居中,但我希望两个文本视图一起居中,以便 "I Am Poor" 居中。
我会将它们放在另一个布局中(与水平方向成线性),然后将另一个布局居中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ONE"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TWO"/>
</LinearLayout>
</RelativeLayout>
RelativeLayout
的现代替代品是 ConstraintLayout
。在 ConstraintLayout 中,您可以创建所谓的 链 视图,然后为整个链定义居中行为。
以下是您的操作方式:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="short"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/large"/>
<TextView
android:id="@+id/large"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="very very very long"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@id/small"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
链是通过将两个视图的开始和结束相互连接并连接到父视图而形成的:
android:id="@+id/small"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/large"
android:id="@+id/large"
app:layout_constraintStart_toEndOf="@id/small"
app:layout_constraintEnd_toEndOf="parent"
然后您还应用 packed
样式,以便视图彼此紧挨着定位,而不是在它们之间展开 space:
app:layout_constraintHorizontal_chainStyle="packed"
最后是这样的结果: