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

时间:2011-08-21 06:25:07

标签: android android-layout

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

list view item layout, what do i whant

布局的高度是项目之间的变化,这里是代码:

<?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>

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

P.S
请注意,使用LinearLayout进行此问题时已解决此问题,但由于布局中的嵌套视图导致性能问题,唯一的解决方案是使用RelativeLayout,TextViews行也在不断更改。

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

我们的想法是采用最上面的视图(按钮)和最底部的视图(可扩展文本),并将它们用作与垂直线高度对齐的锚点。
正如您在此代码中看到的那样:

<?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>    

请注意视图中的更改,尤其是替换“android:marginXXXX = value” 用“android:paddingXXXX = value”来修复视图位置。

谢谢..和美好的一天!

4 个答案:

答案 0 :(得分:1)

好的,我有同样的问题,花了我一个小时才发现。您必须将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,您可以获得所需的结果。

答案 1 :(得分:0)

android:orientation="vertical"标记中删除RelativeLayout行。然后我认为这条线应该是可见的。

答案 2 :(得分:0)

使用android:layout_alignParentTop="true"android:layout_alignParentBottom="true"扩展布局的大小。

  • 不使用android:orientation,将被忽略。

  • 答案 3 :(得分:0)

    作为逻辑您可以使用TableLayout,并且您可以使用视图将每行与水平线分开。 。 。 有关详细信息和Tablelayout,请参阅:Click me