android - 相对布局 : Can't assign a view above a referenced view if margin is involved

标签 android android-layout alignment margin android-relativelayout

我试图将一个 TextView dependent 放在 RelativeLayout 中的另一个 TextView anchor 之上,但我无法使 dependent 得到显示。

情况:
anchor 将与父级的顶部对齐 + 一些 marginTop 使其更靠近父级(RelativeLayout)的中心,以及 dependent将对齐到此 anchor 的上方。

这行不通;当我将它分配到 anchor 上方时,android 似乎假定 anchor 的顶部是父级的顶部,并将 dependent 绘制在屏幕(在它上面)。

这不应该是这种情况,因为我使用 margin 而不是 padding 所以 RelativeLayout 顶部和 anchor 之间的区域应该'成为 anchor 本身的一部分(我使用层次结构查看器检查了大小)。或者我弄错了? :S

这是简单的布局代码:

<?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="fill_parent"
    >

    <TextView android:id="@+id/anchor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="100px"
        android:text="Anchor point."
        />
    <TextView android:id="@+id/dependent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/anchor"
        android:text="Dependent."
        />

</RelativeLayout>

期望:

-------------
|           |
|dependent  |
|anchor     |
|           |
-------------

发生了什么:

dependent (out of screen display)
-------------
|           |
|           |
|anchor     |
|           |
-------------

希望:
有人可以帮我吗?或者如果我犯了错误,也许可以帮助指出。我需要在我的实际实现中使用 RelativeLayout(以上只是一个示例)。

提前致谢。

最佳答案

我做了一个不可见的按钮来对齐 - 这是你要找的吗?如果您更改 anchor 按钮的边距或任何位置参数,它将更改依赖项的位置。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">
<Button android:id="@+id/anchorbutton"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_margin="100dp"
     android:visibility="invisible"
/>

 <TextView android:id="@+id/anchor"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@id/anchorbutton"
  android:layout_alignLeft="@id/anchorbutton"
  android:layout_margin="10dp"
  android:text="Off Anchor button"
/>
<TextView android:id="@+id/dependent"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignLeft="@id/anchorbutton"
  android:layout_below="@id/anchor"
  android:layout_margin="50dp"
  android:text="Dependent."

关于android - 相对布局 : Can't assign a view above a referenced view if margin is involved,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7177076/

相关文章:

android - 电话身份验证在 Firebase 上创建新帐户并断开链接

html - CSS:两个 Logo 左右对齐,应通过窗口调整大小自动缩放

android - 使用库中的示例数据目录

html - ie7的布局问题

html - 将 TD 对齐到 TABLE 的右侧

android - 如果开发人员未指定任何字体,android 中使用的默认字体是什么

java - Sqlite IN 子句在 android 中使用数组中的值不起作用?

javascript - html5 视频标签无法使用 cordova 2.9.0 和 jquery1.9.0 在 phonegap android 中播放 .mp4 视频?

android - 搜索栏的自定义布局或设计

android - 如何在android studio中编写平方根