简体   繁体   English

来自另一个View的Android RelativeLayout alignCenter

[英]Android RelativeLayout alignCenter from another View

I have a RelativeLayout with two children that are also both RelativeLayout s containing a few buttons and things. 我有一个有两个孩子的RelativeLayout ,它们也是包含几个按钮和东西的RelativeLayout 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. 这很容易,只需在第二个中使用android:layout_below="@+id/firstLayout" 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. 我的意思是我想要第二个布局找到第一个布局的中心,并对齐自己,使其中心位于相同的x位置。 I see alignLeft, alignRight, alignTop, and alignBaseline , but no center. 我看到alignLeft, alignRight, alignTop, and alignBaseline ,但没有中心。 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. 我知道我可以将它们包装在另一个大小等于"wrap_content" RelativeLayout ,然后使用centerHorizontal="true"作为第二个。 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”,则可以将两个子布局设置为居中

Sometimes you can set alignLeft and alignRight to the desired view and then use gravity on the element you want centered. 有时您可以将alignLeftalignRight设置为所需的视图,然后在要居中的元素上使用重力。

For example: 例如:

android:layout_below="@+id/firstLayout"
android:layout_alignLeft="@+id/firstLayout"
android:layout_aligntRight="@+id/firstLayout"
android:gravity="center"

If the component is not a TextView (or a view where you don't need the background) you could then wrap it in a FrameLayout with the above properties. 如果组件不是TextView (或者您不需要背景的视图),则可以将其包装在具有上述属性的FrameLayout

<MyView id="@+id/firstLayout" .. other props/>

<FrameLayout 
    ... other props
    android:layout_below="@+id/firstLayout"
    android:layout_alignLeft="@+id/firstLayout"
    android:layout_aligntRight="@+id/firstLayout"
    android:gravity="center" >

    <MyView id="@+id/myAlignedView" ..other props />

</FrameLayout>

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. 如果没有,就像Laranjeiro回答的那样,将两个内容包裹在另一个具有重心的布局中。

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 对我有用的唯一解决方案是使用更大组件的尺寸将两个组件包装在FrameLayout中,并使用属性将第二个组件设置在FrameLayout的中心

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/red_component"
        android:layout_width="100dp"
        android:layout_height="100dp"/>

    <View
        android:id="@+id/blue_component"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_gravity="center" />

</FrameLayout>

For texts I have been using this fe for a vertical-center alignment: 对于文本,我一直使用这个fe进行垂直中心对齐:

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_alignTop="@id/theBigOne"
android:layout_alignBottom="@id/theBigOne"
android:layout_toRightOf="@id/theBigOne"

Just set like this: 就像这样设置:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <View
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/bottomHolder"
        android:layout_alignParentTop="true"
        android:layout_centerInParent = "true"/>

    <View
        android:id="@+id/bottomHolder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"/>

</RelativeLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM