【问题标题】:Layout of type "<fill_parent><wrap_content>"“<fill_parent><wrap_content>”类型的布局
【发布时间】:2011-11-30 12:40:32
【问题描述】:

不确定从这个问题标题中我的意思是否清楚,所以这里有详细信息。

我想要实现的是简单的布局,两个 TextView 控件一个接一个地水平对齐。第一个 TextView 可能包含多行文本,第二个 TextView 将只包含 1 个数字。

第二个 TextView 应始终具有显示其内容所需的宽度,向右对齐,高度应等于第一个 TextView 的高度,以便我可以垂直居中显示数据。第一个 TextView 应该占用剩余空间(宽度)并根据需要垂直拉伸。

示例 1:

  ---------------------------------
  |Small Text.                 123|
  ---------------------------------

示例 2:

  ---------------------------------
  |Long Text starts here ....     |
  |... continues here .......  123|
  |... and finishes here ....     |
  ---------------------------------

水平方向的LinearLayout在这里不好,因为它从左到右放置控件。在这种情况下,我需要先将 wrap_content 设置为第二个 TextView,然后将 fill_parent 设置为第一个 TextView。

使用 RelativeLayout 我也无法获得描述的布局。

我不能在这里使用 layout_weight 方法,因为我不知道第二个 TextView 中的数据长度:在一种情况下它可能是 1 个字符,在另一种情况下可能是 8 个字符。所以在 1 个字符的情况下,我将有未使用的空间。

那么,我有机会构建这样的布局吗?

【问题讨论】:

  • 您是否尝试过为此使用 TableLayout?

标签: android layout


【解决方案1】:

像下面这样的布局应该可以为您解决问题:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:singleLine="false"
        android:text="TextView" />
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="center_vertical|right"
        android:text="TextView" />
</LinearLayout>

【讨论】:

  • 使用 layout_width = "match_parent" 到使用 layout_weight 属性的视图。
【解决方案2】:

这里将有助于使用 minWidth 和 maxWidth 属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    • 2010-09-30
    • 1970-01-01
    • 2013-04-04
    • 1970-01-01
    • 1970-01-01
    • 2011-07-04
    相关资源
    最近更新 更多