androidrelativelayout 两行,右边一列

标签 android android-relativelayout

我试图在左侧实现两行,并带有一个 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/

相关文章:

android - 在主要相对布局中对齐两个相对布局

android文本未显示在按钮中

android - 在相对布局中格式化按钮大小

android - Ant 构建具有依赖项的 Android 项目

android - 当 Activity 启动模式为 singleInstance 时,moveTaskToBack(true) 不起作用

java - 无法在 Android WebView 中加载本地 HTML 文件

android - RelativeLayout 中的循环依赖引起的奇怪的 IllegalStateException

java - 如何设置按钮间距

java - Android - 调用对象时崩溃

android - 打开 USB 调试时拔下手机是否安全?