RelativeLayout differences between 1.5 and 2.1

核能气质少年 提交于 2020-01-01 12:13:18

问题


I've got a ListView with items composed of RelativeLayouts. This is the relevant XML from the list items:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/xx"
        android:gravity="center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_centerInParent="true" 
        android:layout_alignParentLeft="true"/>

    <TextView
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/xx" />

    <TextView
        android:id="@+id/tag"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/xx"
        android:layout_below="@id/title" />

    <TextView
        android:id="@+id/subtitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/tag"
        android:layout_below="@id/title" />

</RelativeLayout>

On Android 2.1 (tested on a Nexus One), this shows the desired behavior: Android 1.5 http://img42.imageshack.us/img42/7668/85324076.png

On Android 1.5 however (tested on a HTC Hero), it shows up like this: Android 1.5 http://img257.imageshack.us/img257/2849/72229324.png

[edit] On 1.6 (emulator), it works as expected as well.

The small grey line on the top left is what shows up in the first pic as "xx", so that should be vertically centered. As far as I can see, the XML dictates this, but for some reason, 1.5 ignores it.

Why is this? I can't find anything about this difference, and I've been brute forcing any combination of layout_center, center, alignParent*, but to no avail...

Can anyone shed some light on this? Thanks!


回答1:


For relative layout layout_gravity is not used.

Also, you're using conflicting attribtues centerInParent and alignParentLeft.

Use only one of them.

You can use layout_centerVertical="true" layout_alignParentLeft="true"




回答2:


RelativeLayout receives many bug fixes in 1.6 and then 2.0 :)




回答3:


For one, judging by the fact that it's broken in the earliest version you're testing with and works as expected in the later versions...sounds like a bug that was fixed.

However, unless I'm oversimplifying because you're only showing a few basic sample screen shots, I would do this with nested LinearLayouts anyway.



来源:https://stackoverflow.com/questions/2850336/relativelayout-differences-between-1-5-and-2-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!