【问题标题】:RelativeLayout positioning issue with first child LinearLayout using margin使用边距的第一个子 LinearLayout 的 RelativeLayout 定位问题
【发布时间】:2013-09-05 21:35:38
【问题描述】:

我目前正在尝试将一些 LinearLayouts(然后填充一些图像)放置在屏幕上的绝对位置上。目前,LinearLayouts 与父 RelativeLayout 的左上角对齐,然后使用边距定位。这适用于所有子 LinearLayouts,除了列表中的第一个。在这种情况下,设置一些边距(例如左边距为 25dp)不会显示任何效果。我希望有人遇到同样的问题并可以提供一些指导。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_page"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout android:id="@+id/ll_first" android:layout_height="wrap_content" android:layout_width="wrap_content"
         android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="25dp" android:layout_marginTop="20dp"></LinearLayout>

    <LinearLayout android:id="@+id/ll_second" android:layout_height="wrap_content" android:layout_width="wrap_content"
         android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="175dp" android:layout_marginTop="30dp"></LinearLayout>

    <LinearLayout android:id="@+id/ll_third" android:layout_height="wrap_content" android:layout_width="wrap_content"
         android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="40dp" android:layout_marginTop="140dp"></LinearLayout>

</RelativeLayout>

编辑: 示例图片http://i.stack.imgur.com/HuYjU.jpg

【问题讨论】:

  • 布局叠加在一起。甚至第一个放在你期望的地方,只是第二个和第三个会在第一个上面绘制,你放在那里的任何东西都会被过度绘制。最好将它们一个接一个地放置。
  • 在下面查看我的评论 .. 不幸的是,这并没有解决问题:/

标签: android android-linearlayout android-relativelayout margins


【解决方案1】:

RelativeLayout 为 Views/ViewGroups 相对于另一个 View/ViewGroup 的定位提供了一些标签。

  • android:layout_below="id"android:layout_above="id"
    这会将 View 置于具有 id 的另一个 View 下方/上方。

  • android:layout_alignRightOf="id"android:layout_alignLeftOf="id"
    这会将视图放在具有 id 的另一个视图的右侧/左侧。

所有标签都像这样具有相同的可用性,我使用 android:layout_below="" 将 View/ViewGroup 设置在另一个下方:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_page"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <--! YOUR LAYOUT AT THE TOP OF YOUR PARENT -->
    <LinearLayout 
         android:id="@+id/ll_first"
         android:layout_height="wrap_content" 
         android:layout_width="wrap_content"

         android:layout_alignParentLeft="true"
         android:layout_alignParentTop="true"></LinearLayout>

    <LinearLayout android:id="@+id/ll_second"
         android:layout_height="wrap_content" 
         android:layout_width="wrap_content"

         android:layout_below="@id/ll_first" ></LinearLayout>

    <LinearLayout 
         android:id="@+id/ll_third" 
         android:layout_height="wrap_content" 
         android:layout_width="wrap_content"

         android:layout_below="@id/ll_second"></LinearLayout>

</RelativeLayout>

现在您可以为每个视图的位置使用一些边距。

【讨论】:

  • 感谢您提醒我这个标签。我前段时间删除了它们,因为通过使用它们我必须定义相对于上述元素的边距。我已将代码恢复为使用此标签并调整了边距。不幸的是,RelativeLayout 的第一个 LinearLayout 子项仍然存在定位问题。 (如果需要,我可以提供代码和屏幕截图。)
  • 一种解决方法是向 RelativeLayout 添加适当的填充以正确定位第一个孩子,但这也会带来一些缺点。
  • @AustrianDude 我刚刚意识到您的 LinearLayouts 中没有方向。你已经解决问题了吗?
  • 感谢您的帮助 我不敢相信我忘记了这一点。在解决此问题的课程中,我认识到导致所描述问题的最后一个错误:在我的源代码中,我将可绘制资源正确添加到所有 LinearLayouts 中,第一个除外。在这种情况下,我将可绘制对象添加到 RelativeLayout。这导致图像始终紧贴左上角。
猜你喜欢
  • 1970-01-01
  • 2011-07-21
  • 2023-03-25
  • 2011-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-19
相关资源
最近更新 更多