2013-11-20 41 views
3

我試圖在Android ListView項目中顯示垂直線。我在單元格的XML中定義它,但是當我在ListView中預覽它時,它不會出現。在ListView項目中使用垂直線

我知道這是一個問題,因爲我在這裏看到很多問題,但沒有看到任何真正的答案。

的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:background="@color/appWhite" > 

    <View android:layout_width="5dp" 
     android:layout_height="match_parent" 
     android:background="@color/appGreen" 
     android:layout_alignParentLeft="true" /> 

    <RelativeLayout 
     android:id="@+id/cell_trip_info_container" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/vertical_bar" > 

    <TextView android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/cell_trip_name" 
     android:paddingTop="20dp" 
     android:paddingBottom="20dp" 
     android:textColor="#2c3e50" 
     android:paddingLeft="10dp" 
     android:text="adfadf" /> 
    <TextView android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/cell_trip_country" 
     android:layout_below="@+id/cell_trip_name" 
     android:textColor="#2c3e50" 
     android:paddingLeft="10dp" 
     android:text="adfadf" /> 
    </RelativeLayout> 

    <RelativeLayout android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:id="@+id/whole_container" 
    android:layout_alignParentRight="true" > 

    <RelativeLayout android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:background="@color/appGreen" 
     android:id="@+id/cell_trip_date_box" 
     android:layout_alignTop="@+id/update_buttons" 
     android:layout_alignBottom="@+id/update_buttons" 
     android:padding="20dp" > 

     <TextView android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/cell_tripstart_date" 
     android:layout_centerHorizontal="true" 
     android:textColor="#fff" /> 
     <TextView android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/cell_date_thru" 
     android:text="@string/thru" 
     android:layout_below="@+id/cell_tripstart_date" 
     android:layout_centerHorizontal="true" 
     android:textColor="#fff" /> 
     <TextView android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/cell_tripend_date" 
     android:layout_below="@+id/cell_date_thru" 
     android:textColor="#fff" 
     android:layout_centerHorizontal="true" /> 

    </RelativeLayout>  

    <RelativeLayout android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:id="@+id/update_buttons" 
     android:visibility="gone" 
     android:layout_toRightOf="@+id/cell_trip_date_box" 
     android:background="#e9e9e9" 
     android:padding="20dp" > 

     <ImageView android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:id="@+id/cell_button_edit_trip" 
     android:src="@drawable/slide_edit" 
     android:adjustViewBounds="true" 
     android:contentDescription="@string/content_description" /> 

     <ImageView android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:id="@+id/cell_button_delete_trip" 
     android:src="@drawable/slide_delete" 
     android:layout_toRightOf="@+id/cell_button_edit_trip" 
     android:adjustViewBounds="true" 
     android:contentDescription="@string/content_description" /> 

    </RelativeLayout> 
    </RelativeLayout> 
</RelativeLayout> 
+1

請包括您的ListView項目的所有XML。 –

回答

-2

變化 RelativeLayout的 到 VerticalLayout的

+5

這不是一回事。 –

0

在你cell_trip_info_containerRelativeLayout你有

android:layout_toRightOf="@+id/vertical_bar" 

你不要有一個名爲vertical_bar

查看ID添加到您的豎線視圖,你應該是好去

<View 
     android:id="@+id/vertical_bar" 
     android:layout_width="5dp" 
     android:layout_height="match_parent" 
     android:background="@color/appGreen" 
     android:layout_alignParentLeft="true" /> 
+0

仍然不工作=/ – jacobduron

+0

http://imgur.com/n8XaatK.jpg是我使用你提供的XML,只改變我說的話(加上改變的字符串和顏色,我沒有) – dymmeh

+0

是的,這是如何它將顯示在佈局的預覽中。如果我實際運行代碼,它不會顯示。另外,如果我在ListView預覽中預覽它,它將不會顯示。令人沮喪... – jacobduron

2

我找到了原因,並在另一篇文章中顯而易見的解決方案/解決方法Here

基本上RelativeLayouts無論出於什麼原因都不會在ListView項中的「match_parent」高度設置中播放。其中一個答案推薦使用LinearLayout代替。所以我剛創建了一個新的File,父視圖是一個LinearLayout。然後,我創建了一個RelativeLayout子項並拋出了其中的所有內容。

這次工作很好,很容易讓我創建我的垂直規則。