水平对齐三个TextView块

我正在尝试在我的Android应用程序中对齐三个textview块。这是这种情况:我的第一个区块(发布日期)在左侧正确对齐,而第三个区块(语言)在右侧正确对齐。现在,我试图将第二个块放在上面两个之间的中间,但是您可以看到它位于第一个块的左侧。

enter image description here This is the code:

     <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/container">

        <!-- BLOCK 1 -->

        <TextView
            android:layout_marginTop="16dp"
            android:id="@+id/releaseDateHeader"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:textColor="#ffffff"
            android:text="Release Date"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
            android:textStyle="bold"/>

        <TextView
            android:layout_below="@id/releaseDateHeader"
            android:id="@+id/releaseDateTextView"
            android:layout_width="wrap_content"
            android:ellipsize="end"
            android:textColor="#ffffff"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            tools:text="ff"/>

        <!-- BLOCK 2 -->

        <TextView
            android:layout_marginTop="16dp"
            android:id="@+id/runtimeHeader"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:textColor="#ffffff"
            android:text="Runtime"
            android:layout_alignRight="@id/releaseDateHeader"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
            android:textStyle="bold"/>

        <TextView
            android:layout_below="@id/runtimeHeader"
            android:id="@+id/runtimeTextView"
            android:layout_width="wrap_content"
            android:ellipsize="end"
            android:textColor="#ffffff"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            tools:text="ff"/>

        <!-- BLOCK 3 -->

        <TextView
            android:layout_marginTop="16dp"
            android:layout_marginRight="16dp"
            android:id="@+id/countryHeader"
            android:layout_alignParentEnd="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:textColor="#ffffff"
            android:text="Language"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
            android:textStyle="bold"/>

        <TextView
            android:layout_below="@id/countryHeader"
            android:id="@+id/countryTextView"
            android:layout_marginRight="16dp"
            android:layout_width="wrap_content"
            android:layout_alignParentEnd="true"
            android:ellipsize="end"
            android:textColor="#ffffff"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            tools:text="ff"/>
评论
  • lest
    lest 回复

    Use layout_weight like this:

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        xmlns:tools="http://schemas.android.com/tools"
        android:orientation="horizontal"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:weightSum="3">
    
      <!-- BLOCK 1 -->
    
      <TextView
          android:id="@+id/releaseDateHeader"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textColor="#000"
          android:layout_weight="1"
          android:text="Release Date"
          android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
          android:textStyle="bold"/>
    
      <TextView
          android:layout_below="@id/releaseDateHeader"
          android:id="@+id/releaseDateTextView"
          android:layout_width="wrap_content"
          android:ellipsize="end"
          android:textColor="#000"
          android:layout_weight="1"
          android:layout_height="wrap_content"
          tools:text="ff"/>
    
      <!-- BLOCK 2 -->
    
      <TextView
          android:id="@+id/runtimeHeader"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textColor="#000"
          android:layout_weight="1"
          android:text="Runtime"
          android:layout_alignRight="@id/releaseDateHeader"
          android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
          android:textStyle="bold"/>
    </LinearLayout>
    

  • 猜不透o
    猜不透o 回复

    采用

    android:layout_toRightOf="@id/desired_id"
    android:layout_toLeftOf="@id/desired_id"
    

    it makes View align its left edge to right edge of desired_id and right edge to left edge of desired_id

    so runtimeHeader should be toRightOf countryHeader and toLeftOf releaseDateHeader. also remove layout_alignRight