1.5和2.1之间的RelativeLayout差异

4

我有一个ListView,其中的项由RelativeLayout组成。这是列表项的相关XML:

<?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>

在Android 2.1上(在Nexus One上测试),这显示了期望的行为:Android 1.5 http://img42.imageshack.us/img42/7668/85324076.png 然而,在Android 1.5上(在HTC Hero上测试),它显示如下:Android 1.5 http://img257.imageshack.us/img257/2849/72229324.png [编辑] 在1.6(模拟器)上,它也像预期的那样工作。
左上角的小灰线是第一张图片中显示为“xx”的内容,因此应该垂直居中。据我所见,XML规定了这一点,但由于某种原因,1.5忽略了它。
为什么会这样呢?我找不到有关这种差异的任何信息,我一直在尝试使用layout_center、center、alignParent*的任意组合,但都无济于事...
能否有人解释一下这是为什么?谢谢!
3个回答

1

相对布局中不使用layout_gravity属性。

此外,您正在使用冲突的属性centerInParentalignParentLeft

只使用其中一个。

您可以使用layout_centerVertical="true" layout_alignParentLeft="true"


是的,那些是我属性暴力破解的剩余物。 :) 修正这个似乎没有帮助。无论如何还是谢谢。 - benvd

1

RelativeLayout在1.6和2.0中得到了许多错误修复 :)


1
很遗憾,据我所知,在比利时大多数设备仍在运行1.5版本,而我正在编写的应用程序主要面向比利时用户。 :( - benvd
@Romain Guy:“1.6版本和2.0版本中修复了许多错误”,但仍然存在许多错误!我更喜欢LinearLayouts,因为它们更稳定、更易于维护,即使这意味着性能的代价。 - JBM

0
首先,根据您正在测试的最早版本中出现故障,而在后来的版本中按预期工作的事实来判断...听起来像是已经修复的错误。
但是,除非我过于简化了,因为您只显示了一些基本的样本屏幕截图,否则我仍然会使用嵌套的LinearLayouts来完成此操作。

1
一个单独的RelativeLayout比嵌套的LinearLayout更好。 - Romain Guy
3
在编程中,我很少在没有上下文的情况下使用“比...更好”的术语。所有编程决策都与许多变量有关。如果性能差异可以忽略不计,那么代码可读性、灵活性、易用性等方面之间存在权衡。在某些情况下,单个RelativeLayout可能会减少代码,从而更易于阅读,但是嵌套的LinearLayout更容易进行小修改,例如插入新元素等,而不影响其他元素。此外,它可能会在1.5中修复这个人的错误...从而使得LinearLayout对于他的情况更好。 - Rich
一个相对布局在内存和性能方面比多个嵌套的线性布局更好。 - gvaish
我实际上是从一个嵌套的LinearLayouts工作视图中来的。但在Hero上,我遇到了StackOverFlowError,所以我优化了很多布局。也许对于这个问题,我应该回到最初的LinearLayouts。 - benvd

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接