2016-12-05 119 views
0

我想知道使用3个TextView进行简单布局的最佳方法。 第一个TextView可能非常长,包含多行,第二个TextView很小,应该在第一个TextView的右侧,第三个TextView在具有固定大小的父级布局的右侧。制作此3视图布局的最佳方式是什么?

下面是这个布局,我需要的两行: enter image description here

这里的示例XML显示错误的结果,因为22号是最右边的行。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:minHeight="56dp" 
    android:orientation="horizontal"> 

<TextView 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="1" 
    android:gravity="center_vertical" 
    android:paddingLeft="16dp" 
    android:paddingRight="16dp" 
    android:text="TextView Row 1" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:gravity="center_vertical" 
    android:text="22" /> 

<TextView 
    android:layout_width="56dp" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:text="show" /> 
</LinearLayout> 
+0

@MikeM。如果TextView 1很长,那么它会打破其他2个视图:https://puu.sh/sEr6Q/fadd545bdc.png –

+0

@MikeM。添加空间,TextView 1仍然占用所有宽度,只是隐藏所有其他视图,如果它太长:https://puu.sh/sErK5/18f44326fd.png –

+0

是的,我没有想到那里。我的错。 –

回答

0

试试这个。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:minHeight="56dp" 
      android:orientation="horizontal"> 

      <TextView 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="2" 
       android:gravity="center_vertical" 
       android:paddingLeft="16dp" 
       android:paddingRight="16dp" 
       android:text="TextView Row 1" /> 

      <TextView 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:gravity="center_vertical" 
       android:layout_weight="1" 
       android:text="22" /> 

      <TextView 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:gravity="center" 
       android:layout_weight="1" 
       android:text="show" /> 
     </LinearLayout> 
+0

如果TextView 1很小,那么第二个TextView将位于中心:https://puu.sh/sEFiK/ca8451bb70.png,但我需要在TextView 1之后。 –

相关问题