【问题标题】:Android RelativeLayout alignCenter from another View来自另一个视图的Android RelativeLayout alignCenter
【发布时间】:2011-07-26 03:55:14
【问题描述】:

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

这是一个粗略的例子,我试图最终得到的结果是,蓝条是第一个布局,红框是第二个布局。我知道我可以将它们都包装在另一个大小等于"wrap_content"RelativeLayout 中,然后将centerHorizontal="true" 用于第二个。但我真的宁愿将两者都保留为我的主要布局的子级(如果我将应用程序的某些部分设为单独布局的子级,我将不得不对它们的工作方式进行一些更改。我试图避免这种情况。)

【问题讨论】:

    标签: android alignment android-relativelayout


    【解决方案1】:

    有时您可以将alignLeftalignRight 设置为所需的视图,然后在要居中的元素上使用重力。

    例如:

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

    如果组件不是 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>
    

    这样,底部视图将是一个与上部相同宽度的框架,但在其中会有一个以宽度为中心的视图。

    当然,这仅在您知道哪个是更大的视图时才有效。如果不是,正如 Laranjeiro 所回答的那样,将这两个内容都包装在另一个具有中心重心的布局中。

    【讨论】:

      【解决方案2】:

      对于我一直使用这个 f.e 进行垂直居中对齐的文本:

      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"
      

      【讨论】:

        【解决方案3】:

        对我有用的唯一解决方案是将两个组件包装在一个具有较大组件尺寸的 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>
        

        【讨论】:

          【解决方案4】:

          就这样设置:

          <?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>
          

          【讨论】:

          • 阅读问题:“这些子布局不在我的主布局中居中”。
          【解决方案5】:

          如果您可以将父布局的宽度设置为“wrap_content”,那么您可以将两个子布局都放在中心

          【讨论】:

          • 我认为你的意思是宽度。但是这种方法行不通,因为父布局中还有其他东西。而这两个孩子实际上是在父母的一边。
          • 是的,我输入宽度。也许你可以多放一个布局,对齐第一个相对布局(蓝色方块)的左右对齐,而不是把红色方块放在中间
          • 这实际上可能有效。我知道我的应用程序的 java 部分可以正常工作,因为现在我需要蓝条布局保持在主布局父级内。但我想如果我只把红色的放在新的父母身上,我就不必做任何改变了。
          • 所以你让我想到了它。我什至认为我不需要使用额外的父布局。我可以在底部布局上使用 alignLeft 和 alignRight ,使其与顶部的宽度相同,然后在其上使用gravity="centerHorizo​​ntal"
          • 是的,但这取决于内部的视图
          猜你喜欢
          • 2012-05-11
          • 2016-11-21
          • 1970-01-01
          • 2014-07-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-07-03
          • 1970-01-01
          相关资源
          最近更新 更多