在“线性水平”布局中在右侧对齐视图的最佳方法是什么?

时间:2018-09-30 06:40:46

标签: android-linearlayout text-alignment margins

如果左侧的视图的长度不一样。假设您具有以下条件:

水平布局1(名称:Mary) 布局2(年龄:28) 布局3(高度:5'4)

我希望右侧的视图对齐,以便右侧每个文本的第一个字母位于同一位置,例如,我知道如何将左侧的textView对齐。在这里,“年龄”在“名称”的正下方,在“高度”的正上方。现在,我希望右边的视图也一样:在“ 2”和“ 5”上方的字母“ M”。

因此,如果在下面我为第一个textView指定了边距,则如果左侧的另一个文本视图具有相同数量的字母(例如“日期”),则可以使用。如果左侧的textView较长,该怎么办?预先谢谢你。

P.S。主持人:我找到了一些类似的答案,但没有一个解决这个特定的问题。

<LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:layout_marginStart="7dp"
                    android:layout_marginLeft="7dp"
                    android:text="Name"/>

                 <TextView
                    android:layout_marginStart="7dp"
                    android:layout_marginLeft="7dp"
                    android:text="Mary"/>
   </LinearLayout>

1 个答案:

答案 0 :(得分:0)

我认为您正在以错误的方式来对待。 尝试使用嵌套的LiniarLayouts

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context=".MainActivity"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="7dp"
            android:layout_marginLeft="7dp"
            android:text="Name"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="7dp"
            android:layout_marginLeft="7dp"
            android:text="Age"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="7dp"
            android:layout_marginLeft="7dp"
            android:text="Height"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="7dp"
            android:layout_marginLeft="7dp"
            android:text="Mary"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="7dp"
            android:layout_marginLeft="7dp"
            android:text="28"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="7dp"
            android:layout_marginLeft="7dp"
            android:text="5'4"/>
    </LinearLayout>

</LinearLayout>