Android 尝试在同一行上左对齐、居中对齐和右对齐3个文本Vue

Android 尝试在同一行上左对齐、居中对齐和右对齐3个文本Vue,android,Android,我试图在同一行上获得3个文本视图,它们是左对齐、居中和右对齐的。它们都在左手边。它们都在一个水平容器中。我用重力把药水放在那里 代码 重力左侧和右侧不适用于水平线性布局,因为它已经从左到右进行了布局。最好将它们包装在一个RelativeLayout中并使用其属性 <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:backg

我试图在同一行上获得3个文本视图,它们是左对齐、居中和右对齐的。它们都在左手边。它们都在一个水平容器中。我用重力把药水放在那里

代码



重力
左侧和右侧不适用于
水平
线性布局
,因为它已经从左到右进行了布局。最好将它们包装在一个
RelativeLayout
中并使用其属性

<RelativeLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#fff7f1d2">
   <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="left"
        android:textStyle="bold"/>    
   <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="center"
        android:layout_centerInParent="true"    <!-- This property -->
        android:textStyle="bold"/>    

   <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="In case of Emergency: Break Glass"
        android:layout_alignParentRight="true"  <!-- And this property -->
        android:textStyle="bold" />    

</RelativeLayout> 

android:textStyle=“bold”/
android:textStyle=“bold”/

默认情况下,第一个
TextView
将向右对齐。

由于线性布局的宽度设置为
fill\u parent
,因此可以更轻松地执行此操作:

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#fff7f1d2"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"a
        android:layout_weight="1"
        android:text="left"
        android:textStyle="bold" /> 

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="center"
        android:textStyle="bold" />    

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="In case of Emergency: Break Glass"
        android:textStyle="bold" />    

</LinearLayout> 


这里的区别是android:layout_weight属性,因为所有同级都具有相同的权重,所以每个
TextView
将占据可用宽度的33%。

使用重力而不是布局重力来定位文本。使用宽度=0dp和重量=1,将所有3个空间均匀隔开
<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#fff7f1d2"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"a
        android:layout_weight="1"
        android:text="left"
        android:textStyle="bold" /> 

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="center"
        android:textStyle="bold" />    

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="In case of Emergency: Break Glass"
        android:textStyle="bold" />    

</LinearLayout>