Android相对布局alignCenter从另一个视图
我有一个带有两个子元素的 RelativeLayout
,它们也是包含一些按钮和内容的 RelativeLayout
。这些子布局并不在我的主布局中居中,并且主布局确实包含这两个布局之外的一些其他内容。我希望第一个位于第二个之上。这很简单,只需在第二个中使用 android:layout_below="@+id/firstLayout"
即可。但我还希望第二个与第一个的中心对齐。我的意思是我希望第二个布局找到第一个布局的中心,并自行对齐,使其中心位于相同的 x 位置。我看到了alignLeft、alignRight、alignTop 和alignBaseline,但没有中心。这是否可以做到,而不必硬编码边距以将第二个布局移动到某些位置?
这是我想要最终得到的一个粗略示例,蓝色条将是第一个布局,红色框将是第二个布局。我知道我可以将它们包装在另一个大小等于 "wrap_content"
的 RelativeLayout
中,然后使用 centerHorizontal="true"
第二个。但我真的宁愿将两者都保留为我的主布局的子级(如果我将它们作为单独布局的子级,我将不得不对应用程序的某些部分的工作方式进行一些更改。我试图避免这种情况。)
I have a RelativeLayout
with two children that are also both RelativeLayout
s containing a few buttons and things. These children layouts are not centered in my main layout, and the main layout does contain some other things off the the sides of these two. I want the first one to be on top of the second one. That is easy enough, just use android:layout_below="@+id/firstLayout"
in the second one. But I also want the second one to be aligned on the center of the first one. By that I mean I want the second layout to find the center of the first one, and align itself so that its center is in the same x position. I see alignLeft, alignRight, alignTop, and alignBaseline
, but no center. Is this possible to do without having to hard code a margin to scoot the second layout over some?
Here is a rough example of what I am trying to end up with, the blue bar would be the first layout, and the red box would be the second layout. I know that I could wrap them both in another RelativeLayout
that has sizes equal to "wrap_content"
and then use centerHorizontal="true"
for the second one. But I would really rather leave both as children of my main layout (I would have to make some changes to the way certain parts of my app work if I make them children of a separate layout. Which I am trying to avoid.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您可以将父布局的宽度设置为“wrap_content”,则可以将两个子布局置于中心
If you can set width of parent layout to "wrap_content" you could then put both children layouts to center
有时,您可以将
alignLeft
和alignRight
设置为所需的视图,然后在要居中的元素上使用重力。例如:
如果组件不是
TextView
(或不需要背景的视图),则可以将其包装在具有上述属性的FrameLayout
中。这样,底部视图将是一个与上部视图具有相同宽度的框架,但在其内部将有一个以宽度为中心的视图。
当然,只有当您知道哪个视图更大时,这才有效。如果没有,正如 Laranjeiro 回答的那样,将这两个内容包装在另一个重心居中的布局中。
Sometimes you can set
alignLeft
andalignRight
to the desired view and then use gravity on the element you want centered.For example:
If the component is not a
TextView
(or a view where you don't need the background) you could then wrap it in aFrameLayout
with the above properties.This way, the bottom view will be a Frame with the same width as the upper one, but inside it there will be a view that is centered to the width.
Of course this only works when you know which is the larger view. If not, as Laranjeiro answered, wrap both contents in another layout with centered gravity.
对于文本,我一直使用此 fe 进行垂直居中对齐:
For texts I have been using this f.e for a vertical-center alignment:
对我有用的唯一解决方案是将两个组件包装在具有较大组件尺寸的 FrameLayout 中,并使用属性将第二个组件设置在 FrameLayout 的中心
The only solution that worked to me was wrap both components in a FrameLayout with the dimension of the bigger component and set the second component in the center of the FrameLayout using the property
只需像这样设置:
Just set like this: