简体   繁体   English

Android Studio布局-在同一行上彼此相邻的两个textview的父级中水平居中

[英]Android Studio Layout - Horizontal centering in parent of two textviews that are next to each other on same line

I have a beginners question about relative layouts and centering of two textviews in Android Studio. 我对Android Studio中两个文本视图的相对布局和居中有一个初学者的问题。 I have two textviews that are aligned on the same line and the right one is aligned "toEndOf" the left textview. 我有两个对齐在同一行上的textview,右边的则是“ toEndOf”左边的textview对齐的。 Now how do I center them both horizontally in the parent so the line of text is centered left to right on the screen? 现在,如何将它们在父母中水平居中,使文本行在屏幕上从左到右居中? See image. 见图片。

在此处输入图片说明

With the given layout, "I Am" is nicely centered horizontally, but I want the both textviews taken together to be centered so that "I Am Poor" is centered. 在给定的布局下,“我是”很好地水平居中,但我希望两个文本视图一起居中,以便“我是穷人”居中。

I would put them inside another layout (as linear with horizontal orientation) and then center that other layout 我将它们放在另一个布局中(与水平方向成线性关系),然后将另一个布局居中

<?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>

The modern replacement for RelativeLayout is ConstraintLayout . RelativeLayout的现代替代品是ConstraintLayout In a ConstraintLayout, you can create what are called chains of views, and then define centering behavior for the entire chain. 在ConstraintLayout中,您可以创建所谓的视图 ,然后定义整个链的居中行为。

Here's how you'd do it: 这是您的处理方式:

<?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>

The chain is formed by connecting both the start and the end of both views to each other and to the parent: 链条通过连接两个开始和两个视图彼此以及连接到母体的端部形成:

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"

You then also apply the packed style so that the views are positioned exactly next to each other, rather than spread out with space between them: 然后,您还可以应用packed样式,以使视图彼此精确地定位,而不是在它们之间留出空间:

app:layout_constraintHorizontal_chainStyle="packed"

In the end, this is the result: 最后,这是结果:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM