如何垂直对齐多个文本视图和多个编辑文本,而不考虑字符串大小字体


我需要做的是将EditText垂直对齐到

中心,并将TextEdit垂直对齐到左侧。

例:苹果_______橙_______西瓜_______

我已经尝试了下面的功能,但它似乎不起作用,因为每个字母的大小字体不同

例:I_____问_____

其中 * 是一个空格,因此编辑文本 ____ 未对齐

//Right padding
    public static String padRight(String s, int n) {
         return String.format("%1$-" + n + "s", s);  
    }

    //Left padding
    public static String padLeft(String s, int n) {
        return String.format("%1$" + n + "s", s);  
    }

 <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="8dp"
            android:gravity="center"
            android:orientation="horizontal" >
            <TextView
                android:id="@+id/textViewUserWeist"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"              
                android:gravity="left"
                android:layout_marginLeft="15dp"
                android:layout_marginStart="15dp"
                android:layout_marginRight="15dp"
                android:layout_marginEnd="15dp"
                android:text="Apple"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/black" />
            <EditText
                android:id="@+id/editWeist"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginStart="15dp"
                android:layout_marginRight="15dp"
                android:layout_marginEnd="15dp"                
                android:gravity="center|top"
                android:hint="..."
                android:background="@drawable/draw_rounded_edittext"
                android:textCursorDrawable="@drawable/draw_color_cursor" />
     </LinearLayout>
            <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="8dp"
            android:gravity="center"
            android:orientation="horizontal" >
            <TextView
                android:id="@+id/textViewUserHips"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"               
                android:gravity="left"
                android:layout_marginLeft="15dp"
                android:layout_marginStart="15dp"
                android:layout_marginRight="15dp"
                android:layout_marginEnd="15dp"
                android:text="Grape"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/black" />
            <EditText
                android:id="@+id/editHips"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginStart="15dp"
                android:layout_marginRight="15dp"
                android:layout_marginEnd="15dp"                
                android:gravity="center|top"
                android:hint="..."
                android:background="@drawable/draw_rounded_edittext"
                android:textCursorDrawable="@drawable/draw_color_cursor" />
           </LinearLayout>
我认为

,据我所知,您要完成什么,将第一个LinearLayout的方向更改为"垂直"应该可以解决问题。无论如何,为了获得更好的性能,您应该改用相对布局来减少视图内膨胀的布局。

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="8dp"
        android:gravity="center"
        android:orientation="vertical" >
...
</LinearLayout>

最新更新