开发者

Android View background layout problem

开发者 https://www.devze.com 2023-03-10 23:00 出处:网络
I am trying to use a empty View to indicate the color for each item in a ListView. The idea is that the rowColor View is just a 3dp wide line that should automatically size to the height of secondLin

I am trying to use a empty View to indicate the color for each item in a ListView.

The idea is that the rowColor View is just a 3dp wide line that should automatically size to the height of secondLine and thirdLine (note that all of the content is set in code including the background color of rowColor and that firstLine and thirdLine are often set to GONE).

It shows up perfectly in Eclipse's Graphical Layout but the rowColor does not show up at all on my phone (running 2.3.3) and all the views are stacked. Any idea how to fix it?

The following code for the list_row was adopted from here. I've provided all the code that I would think works based on that layout, but not even close. Does that layout still work?

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="tru开发者_JAVA百科e"
    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 Activity here: http://pastie.org/2035822

Thanks.


I tested your layout in several emulators (from 2.1 to 2.3.3) and it works fine, with or without setting the first TextView visibility to View.GONE.

It must be either a problem in your code elsewhere or a glitch of your specific device.

As a workaround, you can use:

android:layout_height="0dp"

for your rowColor View. Since you are already defining the position of the top and bottom limits with android:layout_below="@id/firstLine" and android:layout_alignParentBottom="true", the actual value of layout_height is irrelevant.


I ran into this recently, and I found that a View with no content apart from a background color will not appear in a RelativeLayout.

If you can, try converting your layout to a LinearLayout. This seems to work for me.

0

精彩评论

暂无评论...
验证码 换一张
取 消