在Android 1.5上,视图与RelativeLayout重叠问题

4

我在Android 1.5的RelativeLayout中遇到了视图重叠的问题...一切在Android 1.6和更高版本上都很正常。

我知道Android 1.5在RelativeLayout方面存在一些问题,但是我没有在StackOverflow或android beginners组中找到与我的具体问题相关的内容。

我的布局由四个部分组成,每个部分都由一个TextView、一个Gallery和另一个垂直对齐的TextView组成:

正在运行的应用程序
最近使用的应用程序
服务
进程

当这四组项目都显示时,一切正常。然而,我的应用程序允许用户指定某些项目不显示。如果用户关闭“正在运行的应用程序”、“最近使用的应用程序”或“服务”,那么剩余的部分就突然重叠在一起。

这是我的布局代码。我不确定自己做错了什么。当用户关闭一个部分的显示时,我使用了View.GONE可见性设置:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_vertical"
    android:layout_gravity="center_vertical"
    android:background="@null"
>
<!-- Running Gallery View Items -->
<TextView 
    style="@style/TitleText"
    android:id="@+id/running_gallery_title_text_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:paddingLeft="1sp"
    android:paddingRight="10sp"
    android:text="@string/running_title"
/>

<Gallery
    android:id="@+id/running_gallery_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/running_gallery_title_text_id"
    android:spacing="5sp"
    android:clipChildren="false"
    android:clipToPadding="false"
    android:unselectedAlpha=".5"
/>

<TextView 
    style="@style/SubTitleText"
    android:id="@+id/running_gallery_current_text_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/running_gallery_id"
    android:gravity="center_horizontal"
/>

<!-- Recent Gallery View Items -->
<TextView 
    style="@style/TitleText"
    android:id="@+id/recent_gallery_title_text_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/running_gallery_current_text_id"
    android:gravity="left"
    android:paddingLeft="1sp"
    android:paddingRight="10sp"
    android:text="@string/recent_title"
/>

<Gallery
    android:id="@+id/recent_gallery_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/recent_gallery_title_text_id"
    android:spacing="5sp"
    android:clipChildren="false"
    android:clipToPadding="false"
    android:unselectedAlpha=".5"
/>

<TextView 
    style="@style/SubTitleText"
    android:id="@+id/recent_gallery_current_text_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/recent_gallery_id"
    android:gravity="center_horizontal"
/>

<!-- Service Gallery View Items -->
<TextView 
    style="@style/TitleText"
    android:id="@+id/service_gallery_title_text_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/recent_gallery_current_text_id"
    android:gravity="left"
    android:paddingLeft="1sp"
    android:paddingRight="10sp"
    android:text="@string/service_title"
/>

<Gallery
    android:id="@+id/service_gallery_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/service_gallery_title_text_id"
    android:spacing="5sp"
    android:clipChildren="false"
    android:clipToPadding="false"
    android:unselectedAlpha=".5"
/>

<TextView 
    style="@style/SubTitleText"
    android:id="@+id/service_gallery_current_text_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/service_gallery_id"
    android:gravity="center_horizontal"
/>
</RelativeLayout>

我删掉了进程部分的xml,试图让这篇文章更简短一些...

我该怎么做才能让它在Android 1.5上运行?我认为这不仅仅是重新排列xml中的视图,因为当所有内容都显示时它可以正常工作。

1个回答

4
两种可能的解决方案:
  • 尝试将元素的高度设置为0或1像素,将可见性设置为INVISIBLE,而非GONE。
  • 将每个Gallery/TextView包装在一个LinearLayout中,将其设置为wrap_height,并在布局上设置上面/下面,而不是子视图。然后将子元素设置为View.GONE,使用于相对定位的线性布局仍可见,但包裹高度为0。
任何一种解决方案的思想都是确保您从未相对于View.GONE的视图定位某些内容;我怀疑这是您遇到的错误的根源。
话说,您为什么需要在这里使用RelativeLayout?从我能快速看到的内容来看,这里的所有内容似乎都可以轻松适应垂直LinearLayout,并且实际上对于此安排似乎概念上更简单。

谢谢您的建议...我会尝试一下。这是我的纵向布局...横向模式会变得更加复杂,需要几个嵌套的LinearLayout。虽然在纵向模式下我可以使用LinearLayout,但在横向模式下仍然存在同样的问题。 - Justin

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