gpt4 book ai didi

android - 如何在可扩展内容 RelativeLayout 中绘制垂直线

转载 作者:行者123 更新时间:2023-11-29 02:11:27 26 4
gpt4 key购买 nike

我面临着在相对布局内绘制一条垂直线作为列表项布局的问题,如下所示:

list view item layout, what do i whant

layout 的高度会随着 item 的变化而变化,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">

<Button android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="3dip"
android:layout_marginTop="3dip"
android:text="Button"></Button>

<TextView android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button1"
android:layout_toRightOf="@+id/button1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/black"
android:text="TextView"></TextView>

<TextView android:id="@+id/expandable_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:layout_toRightOf="@+id/button1"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/black"
android:text="Some expandable text"></TextView>

<View android:id="@+id/vertical_line"
android:layout_width="5dip"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_marginRight="4dip"
android:background="@color/orange"/>

</RelativeLayout>

问题是垂直线不可见/不工作,有什么建议吗?
提前致谢!

附上
请注意,使用 LinearLayout 时,此问题已得到解决,但由于布局中的嵌套 View ,我遇到了性能问题,唯一的解决方案是使用 RelativeLayout,此外,TextViews 行在项目之间发生变化。

解决方案#1
经过一段时间的搜索,并使用了此处发布的建议答案(谢谢..)
我找到了一个解决方案,看起来很丑,但是由于填充问题
RelativeLayout 使用“fill_parent”高度,而布局高度本身设置为“wrap_content”,这似乎是一个合理的选择。

想法是采用最上面的 View (按钮)和最下面的 View (可扩展文本),并将它们用作与垂直线高度对齐的 anchor 。
正如您在这段代码中看到的:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">

<Button android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:paddingTop="3dip"
android:layout_marginTop="3dip"
android:text="Button"></Button>

<TextView android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button1"
android:layout_toRightOf="@+id/button1"
android:paddingTop="3dip"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/black"
android:text="TextView"></TextView>

<TextView android:id="@+id/expandable_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:layout_toRightOf="@+id/button1"
android:paddingBottom="10dip"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/black"
android:text="Some expandable text"></TextView>

<View android:id="@+id/vertical_line"
android:layout_width="5dip"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/list_item_task_statusbutton"
android:layout_alignBottom="@id/list_item_task_description"
android:layout_marginRight="4dip"
android:background="@color/orange"/>

</RelativeLayout>

请注意 View 中的更改,尤其是“android:marginXXXX=value”的替换使用“android:paddingXXXX=value”来固定 View 位置。

谢谢.. 美好的一天!

最佳答案

好的,我遇到了同样的问题,花了一个小时才找到答案。您必须将 RelativeLayout 放入 LinearLayout 中。所以你的代码应该是这样的。

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

<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<View
android:id="@+id/drawerVerticalLine"
android:layout_width="10dip"
android:layout_height="fill_parent"
android:background="#7fa9c5"/>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<Button android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:paddingTop="3dip"
android:layout_marginTop="3dip"
android:text="Button"></Button>
...
</RelativeLayout>
</LinearLayout>
</LinearLayout>

通过填充或对齐 drawerVerticalLine,您将获得所需的结果。

关于android - 如何在可扩展内容 RelativeLayout 中绘制垂直线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7136766/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com