2011-06-08 140 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。这似乎对我有用。