relativelayout:如果涉及边距,则无法在引用视图上方分配视图

发布于 2024-11-30 23:21:15 字数 1749 浏览 1 评论 0原文

我试图将 TextView dependent 放在相对布局内的另一个 TextView anchor 上方,但我无法设法使 dependent 显示。

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

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

情况不应该是这样,因为我使用 margin 而不是 padding,因此relativelayout顶部和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(上面只是一个例子)。

提前致谢。

I was trying to put a TextView dependent above the other TextView anchor inside a RelativeLayout, but I can't manage to make the dependent get displayed.

Situation:
The anchor would be aligned with the parent's top + some marginTop to make it more to the center of the parent (RelativeLayout), and the dependent will be aligned to be above of this anchor.

This doesn't work; when I assigned it to be above the anchor it seems that android assumes the top of the anchor is the parent's top and draws the dependent outside the screen (above it).

This should not be the case since I use margin instead of padding so the area between the top of the RelativeLayout and the anchor shouldn't be the part of the anchor itself (I checked the size with hierarchy viewer). Or maybe I get it wrong? :S

This is the simple layout code:

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

Desired:

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

What happened:

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

Hope:
Could someone help me here? Or maybe help pointing out if I made a mistake. I need to use the RelativeLayout in my real implementation (above is just an example).

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

椵侞 2024-12-07 23:21:15

我制作了一个隐形按钮来对齐 - 这是您要找的吗?如果更改锚按钮的边距或任何位置参数,它将更改附属按钮的位置。

<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."

I made an invisible button to align from - is this what you're looking for? If you change the margin or any location parameter of the anchorbutton it will change the location of the dependants.

<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."
倾其所爱 2024-12-07 23:21:15

我能问一下为什么你不在最上角(顶部或底部 - 无关紧要)制作锚点,然后从中构建你的视图?这就是我所做的,如下:抱歉 - 我还不能发布图片。

   <?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="100dp"
      android:text="Anchor point."
    />
   <TextView android:id="@+id/dependent"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@id/anchor"
      android:layout_alignLeft="@id/anchor"
      android:text="Dependent."
    />

 </RelativeLayout>

或者,如果您确实想保持原样,只需将 dependent 的对齐方式更改为 android:layout_alignParentTop="true"。您也可以在此处设置边距来影响其位置。这是代码和图片。

<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="100dp"
   android:text="Anchor point."
  />
 <TextView android:id="@+id/dependent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginTop="150dp"
    android:text="Dependent."
  />

</RelativeLayout>

Can I ask why you don't make the anchor at the uppermost corner (top or bottom - doesn't matter) and then build your view from that? That's what I do and that's below: Sorry - I can't post pics yet.

   <?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="100dp"
      android:text="Anchor point."
    />
   <TextView android:id="@+id/dependent"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@id/anchor"
      android:layout_alignLeft="@id/anchor"
      android:text="Dependent."
    />

 </RelativeLayout>

Alternatively, if you really want to keep things as they are, just change the alignment of dependent to android:layout_alignParentTop="true". You can have margin here as well to affect it's placement. Here's the code and pic.

<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="100dp"
   android:text="Anchor point."
  />
 <TextView android:id="@+id/dependent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginTop="150dp"
    android:text="Dependent."
  />

</RelativeLayout>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文