3

下午好机器人!

我正面临一个烦人的布局问题,经过数小时的调查后我无法解释......:/。我创建了一个简化的测试用例,在这里展示。

我有一个简单的列表项布局。

列表项 http://www.freeimagehosting.net/4075a.jpg

lis 项目图片链接

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"
    android:minHeight="96dp">

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="TextView"
    android:id="@+id/textView1" android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_centerVertical="true" android:layout_centerHorizontal="true" />

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="TextView"
    android:id="@+id/textView2" android:layout_alignParentTop="true" />

<ImageView 
    android:layout_width="10dp" 
    android:layout_height="match_parent"
    android:id="@+id/imageView1" 
    android:src="@drawable/errorindicator"
    android:layout_below="@id/textView2" />

</RelativeLayout>

errorindicator 是一个红色矩形,您可以在左侧的图像中看到它。xml 看起来像这样:

    <?xml version="1.0" encoding="utf-8"?>
    <shape shape="rectangle"  xmlns:android="http://schemas.android.com/apk/res/android">
     <solid android:color="@color/ControllingConflictColor" />
     <padding android:left="0dp" android:top="0dp" 
         android:right="0dp" android:bottom="0dp" />
    </shape>

所描述的列表项布局按预期工作。

现在我尝试用如上所述的列表项填充列表视图。结果如下所示:

带有列表项的列表视图 http://www.freeimagehosting.net/4cc4b.jpg

列表查看图片链接

对应的xml:

    <?xml version="1.0" encoding="utf-8"?>
    <ListView android:id="@+id/listView1" android:layout_width="fill_parent"
        android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- Preview: listitem=@layout/test -->
    </ListView>

正如您在图像上看到的那样,红色矩形不再像它应该的那样在高度上匹配它的父级。图像是由设计师制作的,但同样的效果也在模拟器和设备上。我正在为 API Level 8 开发。

**如果有人能解释一下,为什么所描述的布局在列表视图中不能按预期工作,我会很高兴。为什么形状可绘制的行为不同?**

感谢您的时间 :)

编辑:我嵌入图像时遇到问题,我使用了链接,抱歉;(编辑:添加了 xml-drawable 标记。

编辑:我的例子甚至可以变得更容易。我包含了两个文本视图,因为它与我的真实布局有些相似。您可以从示例中删除两个文本视图,但仍然存在问题,如果在列表视图中使用布局,则图像视图中的可绘制形状与定义的父级高度不匹配。

4

3 回答 3

0

我还发现使用RelativeLayout 很难完成它。那么试试这个布局为你的行 xml。它使用线性布局。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical">
    <TextView android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="TextView" 
        android:id="@+id/textView2" 
        android:layout_alignParentTop="true" />
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <LinearLayout android:layout_width="10dip" 
            android:layout_height="fill_parent"
            android:background="@drawable/errorindicator"/>       
        <TextView android:layout_width="0dip" 
            android:layout_height="wrap_content" 
            android:text="TextView" android:layout_weight="1.0" 
            android:id="@+id/textView1"
            android:textAppearance="?android:attr/textAppearanceMedium" 
            android:gravity="center"/>
   </LinearLayout>
</LinearLayout>
于 2011-10-18T17:39:55.443 回答
0

因为我猜我想要的行为是不可能使用给布局管理器的,所以我采用了另一种适合我个人情况的方式。

我已经覆盖了 RelativeLayout 的 onDraw 方法并自己绘制了这个红色矩形(在布局过程完成并设置了高度之后)......

于 2011-10-19T10:17:57.557 回答
0

如果您将资源设置为 png,则可以对其进行 9-patch 并将其添加为 RelativeLayout 的背景

于 2011-10-19T12:55:31.500 回答