2015-02-08 61 views
0

我正在尝试创建一个Relative Layout,它具有一个图标,其中下方有一条垂直线,文本位于右侧。该文本将是一个段落,所以该行应该与文本减去图标的大小一样长。现在我的布局正确显示了图标和文本,但我根本没有看到垂直线。这是我的xml:相对布局定位

<?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="match_parent" 
    android:padding="0dp"> 

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingStart="2dp" 
    android:paddingEnd="2dp" 
    android:id="@+id/icon" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentStart="true" /> 

<View 
    android:layout_width="2dp" 
    android:layout_height="match_parent" 
    android:paddingStart="2dp" 
    android:paddingEnd="2dp" 
    android:background="@android:color/darker_gray" 
    android:id="@+id/view" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentStart="true" 
    android:layout_below="@+id/icon" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingStart="2dp" 
    android:paddingEnd="2dp" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:text="Medium Text" 
    android:id="@+id/content" 
    android:layout_alignParentTop="true" 
    android:layout_toEndOf="@+id/icon" /> 
</RelativeLayout> 

我想,我会额外的填充添加到我的View居中一次我真正得到它,以显示

enter image description here

+1

你可以附上一张现在看起来像的图片,以便更容易理解你的帖子吗? – 2015-02-08 23:36:34

+0

您是否尝试过使用线性布局,在我看来,使用它会更容易,但请提供您想要实现的样机或图像! – Necronet 2015-02-08 23:45:28

+0

我附上了它目前看起来像的图像。我正在考虑线性布局来获得两列,但我认为相对布局会更有意义 – 2015-02-09 00:04:31

回答

0

这个问题是因为填充您添加到垂直线。基本上你有一个2dp的视图宽度,但你有4dp的填充,所以没有显示。将您的视图标签更新为:

<View 
    android:layout_width="2dp" 
    android:layout_height="match_parent" 
    android:background="@android:color/darker_gray" 
    android:id="@+id/view" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentStart="true" 
    android:layout_below="@+id/icon" /> 

您发布的布局似乎不会按照您的要求进行。试试这个:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="0dp"> 

    <LinearLayout 
     android:id="@+id/left_container" 
     android:orientation="vertical" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent"> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:paddingStart="2dp" 
      android:paddingEnd="2dp" 
      android:id="@+id/icon"/> 

     <View 
      android:layout_width="2dp" 
      android:layout_height="match_parent" 
      android:background="@android:color/darker_gray" 
      android:id="@+id/view" 
      android:layout_gravity="center_horizontal" /> 

    </LinearLayout> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingStart="2dp" 
     android:paddingEnd="2dp" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Medium Text" 
     android:id="@+id/content" 
     android:layout_alignParentTop="true" 
     android:layout_toEndOf="@+id/left_container" /> 
+0

这产生了与我的原始代码相同的结果。图标和文字处于相同的位置,但没有垂直线 – 2015-02-09 13:13:14

+0

尝试将垂直线的背景颜色更改为类似红色的颜色,以查看它是否与background.android:background="@android相同:颜色/红色“ – eclipse1203 2015-02-09 18:16:48

+0

是的,无论什么颜色我改变它仍然不显示 – 2015-02-09 20:43:06