从另一种观点认为Android的相对布局alignCenter布局、观点、Android、alignCenter

2023-09-11 07:47:23 作者:享受人生

我有两个孩子,同时也是包含几个按钮和东西两个RelativeLayouts一个相对布局。这些儿童的布局中居中在我的主要布局,并主要布局确实包含一些其它的东西掉这两项的两侧。我想第一个是对第二个顶部。这是很容易,只要使用安卓layout_below =@ + ID / firstLayout中的第二个。但我也想第二个被放在了第一位的中心对齐。我的意思是我希望第二个布局,找到第一个中心,并调整自身,以便其中心是在相同的x位置。我看到alignLeft,alignRight,alignTop和alignBaseline,但没有中心。这是可以做到的,而不必硬codeA保证金飞奔第二布置了一些?

I have a relative layout 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?

下面是什么,我想结束了一个粗略的例子,蓝条将是第一个布局,而红色框中将是第二个布局。我知道我可以在另一个相对布局,有大小包装他们都等于WRAP_CONTENT,然后使用centerHorizo​​ntal =真的第二个。但我真的宁可离开无论是作为我的主要布局的孩子(我将不得不作出一些改变的方式我的应用程序工作的某些部分,如果我让他们单独布置的孩子。而我试图避免的。)

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 Relative layout 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