Android View背景布局问题



我正在尝试使用一个空视图来指示ListView中每个项目的颜色。

其想法是,rowColor视图只是一条3dp宽的线,应该自动调整到secondLine和thirdLine的高度(请注意,所有内容都在代码中设置,包括rowColor的背景色,并且firstLine和third line通常设置为GONE)。

它完美地显示在Eclipse的Graphical Layout中,但rowColor根本没有显示在我的手机上(运行2.3.3),所有视图都是堆叠的。知道怎么修吗?

list_row的以下代码就是从这里开始采用的。我已经提供了所有我认为可以基于该布局工作的代码,但还没有接近。这种布局还能用吗?

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

谢谢。

我在几个模拟器中测试了您的布局(从2.1到2.3.3),无论是否将第一个TextView可见性设置为View.GONE.,它都能很好地工作

这一定是你其他地方的代码有问题,或者是你特定设备的故障。

作为一种变通方法,您可以使用:

android:layout_height="0dp"

用于rowColor视图。由于您已经使用android:layout_below="@id/firstLine"android:layout_alignParentBottom="true"定义了上限和下限的位置,因此layout_height的实际值无关紧要。

我最近遇到过这个问题,我发现除了背景颜色之外没有内容的View不会出现在RelativeLayout中。

如果可以,请尝试将布局转换为LinearLayout。这似乎对我有效。

最新更新