不确定这个问题标题是否清楚我的意思,所以这里有详细信息。
我想要实现的是简单的布局,其中两个 TextView 控件依次水平对齐。第一个 TextView 可能包含几行文本,第二个 TextView 将只包含一个数字。
第二个 TextView 的宽度应始终与显示其内容所需的一样多,并向右对齐,高度应等于第一个 TextView 的高度,以便我可以垂直居中显示数据。第一个 TextView 应占用剩余空间(用于宽度)并根据需要垂直拉伸(stretch)。
示例 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 个字符的情况下,我将有未使用的空间。
那么,我有机会构建这样的布局吗?
最佳答案
像下面这样的布局应该可以解决问题:
<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>
关于android - 类型 "<fill_parent><wrap_content>"的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8326114/