【问题标题】:Android Studio Layout - Horizontal centering in parent of two textviews that are next to each other on same lineAndroid Studio 布局 - 在同一行上相邻的两个文本视图的父级中水平居中
【发布时间】:2019-10-19 11:31:23
【问题描述】:

我有一个关于 Android Studio 中两个文本视图的相对布局和居中的初学者问题。我有两个在同一行上对齐的文本视图,右侧对齐左侧文本视图的“toEndOf”。现在我如何将它们都水平居中在父级中,以便文本行在屏幕上从左到右居中?见图片。

对于给定的布局,“我是”很好地水平居中,但我希望两个文本视图一起居中,以便“我是穷人”居中。

【问题讨论】:

    标签: android android-studio android-layout textview


    【解决方案1】:

    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 样式,以便视图彼此紧挨着放置,而不是在它们之间留有空间:

    app:layout_constraintHorizontal_chainStyle="packed"
    

    最后,结果是这样的:

    【讨论】:

    • ConstraintLayout 的语法肯定比RelativeLayout 更复杂,但它的功能要强大得多,而且通常是全方位的更好。我建议尽早习惯使用它。
    • 谢谢本!我会在早上试试这个。感谢您指出更现代的这种布局方式。
    【解决方案2】:

    我会将它们放在另一个布局中(与水平方向成线性),然后将另一个布局居中

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-27
      • 2016-11-25
      • 1970-01-01
      • 2014-03-08
      • 1970-01-01
      • 2016-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多