2011-06-08 88 views
0

我想使用空視圖來指示ListView中每個項目的顏色。Android查看背景佈局問題

這個想法是,rowColor View只是一個3dp寬線,應該自動調整大小到secondLine和thirdLine的高度(請注意,所有內容都是在代碼中設置的,包括rowColor的背景色以及firstLine和thirdLine通常設置爲GONE)。

它在Eclipse的圖形佈局中顯示得很完美,但rowColor根本不顯示在我的手機上(運行2.3.3)並且所有視圖都堆疊在一起。任何想法如何解決它?

以下代碼爲list_row從here採用。我已經提供了所有我認爲基於該佈局工作的代碼,但並未完全關閉。這種佈局是否仍然有效?

list_row.xml:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="?android:attr/listPreferredItemHeight" 
android:padding="6dip"> 

<TextView 
    android:id="@+id/firstLine" 

    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 

    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 

    android:singleLine="true" 
    android:text="First Line" /> 

<View 
    android:id="@+id/rowColor" 
    android:background="#FF0000" 

    android:layout_width="3dp" 
    android:layout_height="fill_parent" 

    android:layout_below="@id/firstLine" 
    android:layout_alignParentBottom="true" /> 

<TextView 
    android:id="@+id/thirdLine" 

    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 

    android:layout_toRightOf="@id/rowColor" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 

    android:singleLine="true" 
    android:text="Third Line" /> 

<TextView 
    android:id="@+id/secondLine" 

    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 

    android:layout_toRightOf="@id/rowColor" 
    android:layout_alignParentRight="true" 
    android:layout_below="@id/firstLine" 
    android:layout_above="@id/thirdLine" 
    android:layout_alignWithParentIfMissing="true" 

    android:gravity="center_vertical" 
    android:singleLine="true" 
    android:text="Second Line" /> 
</RelativeLayout> 

的main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<ListView 
    android:id="@+id/android:list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"/> 
</LinearLayout> 

ListViewTest.java活動在這裏:http://pastie.org/2035822

感謝。

+0

也許嘗試「wrap_content」,看看是什麼 – Austin 2011-06-08 01:00:04

+0

它顯示過嗎?還是隻有當firstList是GONE時纔會失蹤 – jkhouw1 2011-06-08 01:15:22

+0

好吧rowColor從來沒有內容,只是背景顏色。它不會顯示爲wrap_content或match_parent,或firstLine爲GONE時。當我將它的高度設置爲40dp時,我只會看到它。 – kevin 2011-06-08 01:22:04

回答

0

我在幾個模擬器(從2.1到2.3.3)中測試了你的佈局,它可以正常工作,無論是否將第一個TextView可見性設置爲View.GONE。

它必須是您的代碼在其他地方的問題或您的特定設備的小故障。

作爲一種變通方法,您可以使用:

android:layout_height="0dp" 

您rowColor查看。由於您已經使用android:layout_below="@id/firstLine"android:layout_alignParentBottom="true"定義了頂部和底部限制的位置,因此layout_height的實際值無關緊要。

+0

你究竟如何測試它?正如我所說,它在圖形佈局中看起來很完美,但在我的手機上看起來不錯。我發佈的內容實際上是我正在使用的簡化版本,但現在我遇到了問題,即使顯示正確。我會在我的問題中添加更多代碼。 – kevin 2011-06-08 03:27:15

+0

我會說**添加少**,**不多**代碼。修剪它直到你發現什麼是錯的。我在Eclipse中粘貼了佈局,並將其添加到虛擬項目中。然後我如上所述通過多個仿真器運行它,而不僅僅是IDE中的圖形佈局。 – Aleadam 2011-06-08 03:32:29

+0

我希望你已經在一個ListView中使用它。我提供了應該使其工作的附加代碼,但它沒有。有什麼想法嗎? – kevin 2011-06-08 19:41:25

0

我最近碰到了這個,我發現View除了背景色之外沒有內容不會出現在RelativeLayout

如果可以,請嘗試將您的佈局轉換爲LinearLayout。這似乎對我有用。