使用两个TextView进行布局

时间:2012-03-08 02:56:08

标签: android layout android-layout

我正在尝试使用两个TextView设计布局:

|                                                     |
| (Multilined Textview) (60dp TextView)               |
| (of unknown size    )                               |

如果第一个TextView文本很少,它应该如下所示:

|                                                     |
| (Hello!) (12:34:56)                                 |
|                                                     |

如果第一个TextView有很多文本,它应该如下所示:

|                                                     |
| (Ey! This is a very very very very long) (12:34:56) |
| (message.                              )            |

有人知道如何实现这个目标吗?

非常感谢。

编辑:我搞定了。解决方案在我自己的答案中。

4 个答案:

答案 0 :(得分:3)

我相信你可以通过加权LinearLayout来实现这一目标:

<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:weightSum="1">

  <TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1" />

  <TextView
    android:layout_width="60dp"
    android:layout_height="wrap_content"
    android:layout_weight="0" />

</LinearLayout>

更新:您可能还想尝试的替代解决方案(甚至可能与加权LinearLayout结合使用):

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxWidth="200dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

答案 1 :(得分:1)

试试这个:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >    
    <TextView
        android:id="@+id/text_1"
        android:layout_width="wrap_content" 
        android:padding_right = 'fix value of width of TextView text 2'
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:gravity="left" />
    <TextView
        android:id="@+id/text_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/text_1"
        android:gravity="center" />
</RelativeLayout>

答案 2 :(得分:0)

您应该使用android:layout_width="wrap_content"。 如果使用空字符串初始化textview,则应使用android:minHeight="100dp"来设置最小化宽度。

答案 3 :(得分:0)

我在这里提出了最终解决方案:

<TextView
    android:id="@+id/variable_size_textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:paddingRight="60sp" />

<TextView
    android:id="@+id/hack"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_toRightOf="@+id/variable_size_textview" />

<TextView
    android:id="@+id/fixed_size_textview"
    android:layout_width="60sp"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/hack" />

相关问题