Android RelativeLayout alignCenter从另一个视图 [英] Android RelativeLayout alignCenter from another View

查看:59
本文介绍了Android RelativeLayout alignCenter从另一个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 RelativeLayout ,有两个孩子,两个孩子也都是 RelativeLayout ,其中包含一些按钮和其他东西。这些子布局不在我的主布局中居中,并且主布局的确在这两个侧面之外还包含其他一些内容。我希望第一个在第二个之上。这很容易,只需在第二个中使用 android:layout_below = @ + id / firstLayout 。但我也希望将第二个对齐到第一个的中心。我的意思是说,我希望第二个布局找到第一个布局的中心,并使其自身对齐,以使其中心位于相同的x位置。我看到 alignLeft,alignRight,alignTop和alignBaseline ,但没有居中。不必硬编码一个边距以在某些位置上覆盖第二个布局就可以做到这一点吗?

I have a RelativeLayout with two children that are also both RelativeLayouts 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?

以下是我要最终解决的一个粗糙示例,蓝色栏将是第一个布局,红色框将是第二个布局。我知道我可以将它们都包装在另一个 RelativeLayout 中,其大小等于 wrap_content ,然后使用 centerHorizo​​ntal = true 。但是我真的希望两个都作为我的主要布局的孩子(如果我让它们的孩子成为单独布局的孩子,我必须对应用程序某些部分的工作方式进行一些更改。我想避免这种情况。)

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

推荐答案

如果您可以将父版式的宽度设置为 wrap_content,则可以将两个子版式的居中放置

If you can set width of parent layout to "wrap_content" you could then put both children layouts to center

这篇关于Android RelativeLayout alignCenter从另一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆