relativelayout:如果涉及边距,则无法在引用视图上方分配视图
我试图将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我制作了一个隐形按钮来对齐 - 这是您要找的吗?如果更改锚按钮的边距或任何位置参数,它将更改附属按钮的位置。
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.
我能问一下为什么你不在最上角(顶部或底部 - 无关紧要)制作锚点,然后从中构建你的视图?这就是我所做的,如下:抱歉 - 我还不能发布图片。
或者,如果您确实想保持原样,只需将 dependent 的对齐方式更改为 android:layout_alignParentTop="true"。您也可以在此处设置边距来影响其位置。这是代码和图片。
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.
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.