relativeLayout 的行为与列表视图项不同
机器人们下午好!
我面临着一个恼人的布局问题,经过数小时的调查后我没有任何解释......:/。 我创建了一个简化的测试用例,在这里展示。
我有一个简单的列表项布局。
列表项 http://www.freeimagehosting.net/4075a.jpg
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>
错误指示器是一个红色矩形,您可以在左侧的图像上看到它。 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 级别 8 进行开发。
**如果有人可以向我解释为什么所描述的布局在列表视图中不能按预期工作,我希望能有人能解释一下。为什么可绘制的形状表现不同? **
谢谢您的时间:)
编辑:我在嵌入图像时遇到问题,我使用了链接,抱歉;( 编辑:添加了 xml-drawable 标签。
编辑:我的例子甚至可以变得更加容易。我包含了两个文本视图,因为它与我的真实布局有一些相似之处。您可以从示例中删除两个文本视图,但仍然存在问题,如果在列表视图中使用布局,则图像视图中可绘制的形状与定义的父级高度不匹配。
Good afternoon droids!
I am facing an annoying layout problem to which i have no explanation after hours and hours of investigation... :/. I created a reduced test case which i present here.
I have a simple list item layout.
list item http://www.freeimagehosting.net/4075a.jpg
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>
the errorindicator is a red rectangle, which you can see on the image on the left side. The xml looks like that:
<?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>
The described list item layout works as expected.
Now i try to fill a list view with list items as described above. The result looks like that:
list view with list items http://www.freeimagehosting.net/4cc4b.jpg
The corresponding 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>
As you can see on the image, the red rectangle does not match it's parent in the height anymore as it should. The images are made from the designer, but the same effect is also on the emulator and device. I am developing for API Level 8.
**I'd love if somebody could explain me, why the described layout does not work as expected in a list view. Why does the shape drawable behave different? **
Thank you for your time :)
edit: i have problems embedding the image, i used a link instead, sorry ;(
edit: added xml-drawable tag.
edit: My example can be even made much more easier. I included two text views because it has some similarity to my real layout. You can remove the two text views from the example and there still exists the problem, that the shape drawable in the image view does not match the parents height as defined, if the layout is used in a list view.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我还发现很难使用RelativeLayout 来完成它。我们为您的行 xml 尝试一下这种布局。它使用线性布局。
Im also finding it hard to get it done using a RelativeLayout. Well try this layout for your row xml. It uses LinearLayout.
因为我猜想给定的布局管理器不可能实现我想要的行为,所以我采用了另一种适合我个人情况的方法。
我已经重写了RelativeLayout的onDraw方法并自己绘制了这个红色矩形(在布局过程完成并设置了高度之后)...
Because i guess my desired behaviour is not possible with the give layout manager, i went another way which fits my personal situation.
I have overriden the onDraw method of the RelativeLayout and paint this red rectangle myself (after the layout process finished and the heights are set)...
如果您将资源制作为 png,则可以对其进行 9 修补并将其添加为relativelayout 的背景
If you made the asset as a png, you could then 9-patch it and add it as the background of the RelativeLayout