【问题标题】:RelativeLayout differences between 1.5 and 2.11.5 和 2.1 之间的 RelativeLayout 差异
【发布时间】:2010-06-08 13:48:17
【问题描述】:

我有一个 ListView,其中包含由 RelativeLayouts 组成的项目。这是列表项中的相关 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

[edit] 在 1.6(模拟器)上,它也可以正常工作。

左上角的灰色小线在第一张图片中显示为“xx”,因此它应该垂直居中。据我所知,XML 规定了这一点,但由于某种原因,1.5 忽略了它。

这是为什么?我找不到关于这种差异的任何信息,并且我一直在强制使用 layout_center、center、alignParent* 的任何组合,但无济于事......

任何人都可以对此有所了解吗?谢谢!

【问题讨论】:

    标签: android listview android-relativelayout


    【解决方案1】:

    对于相对布局layout_gravity不使用。

    另外,您使用了相互冲突的属性 centerInParentalignParentLeft

    只使用其中一个。

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

    【讨论】:

    • 是的,这些是我的属性暴力破解的剩余部分。 :) 纠正这个似乎没有帮助。还是谢谢。
    【解决方案2】:

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

    【讨论】:

    • 遗憾的是,据我所知,在比利时,大多数设备仍然运行 1.5,而我正在编写的应用程序主要针对比利时用户。 :(
    • @Romain Guy “在 1.6 和 2.0 中修复了许多错误”,但仍然包含许多错误!我更喜欢 LinearLayouts,因为它具有更好的稳定性和更易于维护的特性,即使它带有性能价格。
    【解决方案3】:

    首先,从它在您正在测试的最早版本中被破坏并且在更高版本中按预期工作的事实来看...听起来像是一个已修复的错误。

    但是,除非我过于简单化,因为您只显示几个基本的示例屏幕截图,否则无论如何我都会使用嵌套的 LinearLayouts 来做到这一点。

    【讨论】:

    • 单个RelativeLayout比嵌套的LinearLayout好很多。
    • 在编程中,我很少在没有上下文的情况下使用“优于”一词。所有的编程决策都与许多变量有关。如果性能差异可以忽略不计,那么就需要在代码可读性、灵活性、易用性等方面进行权衡。单个 RelativeLayout 可能代码更少,因此在某些情况下更具可读性,但嵌套的 LinearLayout 更容易进行小的修改例如插入新元素等不影响其他元素。此外,它可以很好地修复这个人在 1.5 中的错误...使 LinearLayouts 立即更适合他的情况。
    • 一个相对布局在内存和性能方面优于多个嵌套线性布局。
    • 我实际上是来自一个带有嵌套 LinearLayouts 的工作视图。 :) 但是在 Hero 上,我得到了 StackOverFlowError,所以我优化了很多布局。也许对于这个我应该回到原来的,使用 LinearLayouts。
    猜你喜欢
    • 2011-06-18
    • 2011-11-04
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-13
    • 1970-01-01
    • 2010-10-19
    相关资源
    最近更新 更多