我试图在左侧实现两行,并带有一个 float (右侧)TextView,其中有一个数字。这是一个 ListView 项目布局。
____
___________________________ | |
___________________________ |____|
所以内容会是这样的
LINE ONE CAN SOMETIMES BE LONG £2000.00
line two
但是 £2000.00(较大的文本大小)要么与下面的代码示例重叠,要么使用 android:layout_toRightOf="@+id/row1"
它基本上位于 row1 旁边,有时如果 row1 很长,则转到下一行。它应该始终排成一行。
有人可以帮忙吗?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="6dp" >
<TextView
android:id="@+id/row1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold" />
<TextView
android:id="@+id/rightcol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textColor="#cc0000"
android:textSize="22dp" />
<TextView
android:id="@+id/row2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/row1" />
</RelativeLayout>
这样右列文本就会与 row1 重叠
感谢您的查看
最佳答案
android:layout_weight
对于实现您想要的布局非常有用(请参阅 this )。您可以使用 android:layout_width="wrap_content"
作为右侧 TextView ,而不是 50dp。其他 TextView 将调整以填充剩余空间。您可以使用 android:singleLine="true"
来限制 TextView,使其不换行。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<TextView
android:layout_width="50dp"
android:layout_height="50dp"/>
</LinearLayout>
如果您决定使用RelativeLayout,这应该可行。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="6dp" >
<TextView
android:id="@+id/row1"
android:text="This text can be very, very long"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/rightcol"
android:textStyle="bold" />
<TextView
android:id="@+id/rightcol"
android:text="$2000000000000.00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textColor="#cc0000"
android:textSize="22dp" />
<TextView
android:id="@+id/row2"
android:text="Second line of text"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/rightcol"
android:layout_below="@+id/row1" />
</RelativeLayout>
关于androidrelativelayout 两行,右边一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27476878/