【问题标题】:RelativeLayout & a vertical divider aligned on the right相对布局和右侧对齐的垂直分隔线
【发布时间】:2015-08-21 01:14:43
【问题描述】:

无法进行简单的 RelativeLayout。我正在构建一个由如下所示的行组成的 ListView:

    +-----------------------------+--------------+
    | Line 1                      | Small button |
    | Line 2, more text           |              |
    | Line 3, even more text      |              |

这是我的第一次尝试 - 不幸的是,垂直分隔线没有出现。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:descendantFocusability="blocksDescendants">

    <TextView
        android:id="@+id/lineOne"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:textColor="@color/wallet_holo_blue_light"
        android:textSize="18dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/lineTwo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/lineOne"
        android:textSize="16dp" />

    <TextView
        android:id="@+id/lineThree"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/lineTwo"
        android:textSize="14dp" />

    <ImageButton
        android:id="@+id/myButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="@android:color/transparent"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:onClick="doSomething"
        android:src="@drawable/arrow_down" />

    <View
        android:layout_width="1dp"
        android:layout_height="fill_parent"
        android:layout_toLeftOf="@id/myButton"
        android:background="?android:attr/listDivider" />

</RelativeLayout>

编辑:我几乎用下面的代码解决了我自己的问题。

现在我的布局看起来不错...

+-----------------------------+--------------+
| Line 1                      | Small button |
| Line 2, more text           |              |
| Line 3, even more text      |              |

... 除非例如第一行太长 - 在这种情况下,分隔符和按钮不会显示:

+-----------------------------+--------------+
| Line 1, way way way way too long which     |
| wraps                                      |
| Line 2, more text                          |
| Line 3, even more text                     |

您能帮我做最后的调整吗?

非常感谢!

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:descendantFocusability="blocksDescendants"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/lineOne"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/wallet_holo_blue_light"
            android:textSize="18dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/lineTwo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16dp" />

        <TextView
            android:id="@+id/lineThree"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="14dp" />

    </LinearLayout>

    <!-- As per http://stackoverflow.com/questions/6992804/how-to-right-align-widget-in-horizontal-linear-layout-android -->
    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1" />

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

        <View
            android:layout_width="1dp"
            android:layout_height="fill_parent"
            android:background="?android:attr/listDivider" />

        <ImageButton
            android:id="@+id/myButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:src="@drawable/arrow_down" />

    </LinearLayout>

</LinearLayout>

【问题讨论】:

  • 我查看了您的文件,应该没问题。我只查看了预览,但在我的情况下显示了垂直分隔线。您在预览中没有看到任何分隔线?还是也只是预览版?
  • 您还应该考虑将layout_height 设置为wrap_content,因为它们是列表视图项。
  • 感谢您对此进行调查!我确实在预览中看到了分隔线,但在我的手机上没有。

标签: android


【解决方案1】:
+-----------------------------+--------------+
| Line 1                      | Small button |
| Line 2, more text           |              |
| Line 3, even more text      |              |

如果你想实现这样的布局,那么首先考虑哪些是固定长度,哪些是可变的。我假设分隔线和小按钮的宽度总是很小,只有线宽会改变。因此,在这种情况下,您可以设置按钮和分隔线宽度,并将其余宽度设置为线条。您可以通过在 Layout 中使用 layout_weight="1" 来做到这一点,这将获得剩余空间。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:descendantFocusability="blocksDescendants"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        //your lines here
    </LinearLayour>

    <View
        android:layout_width="1dp"
        android:layout_height="fill_parent"
        android:background="?android:attr/listDivider" />
    <ImageButton
        android:id="@+id/myButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:src="@drawable/arrow_down" />
</LinearLayour>

【讨论】:

    猜你喜欢
    • 2012-08-27
    • 2012-06-25
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 2019-02-25
    • 2021-06-13
    • 1970-01-01
    相关资源
    最近更新 更多