2015-02-08 74 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