Android 相对布局重心不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10904864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:23:31  来源:igfitidea点击:

RelativeLayout gravity center not working

androidcenterandroid-relativelayoutgravity

提问by Frank Sposaro

I'm trying to horizontally center several views in a RelativeLayoutthat is a base.

我试图将几个视图水平居中在RelativeLayout一个基础上。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"
    android:background="@android:color/transparent" >

This isn't working. I have set centerInParentto truefor one of the views and that did work. However, I can't use this solution because I have 2 views side by side that need to be centered together. Trying to optimize this so I want to avoid nesting layouts, especially Linear, inside of each other.

这不起作用。我已centerInParenttrue了一个视图,并且做了工作。但是,我无法使用此解决方案,因为我有 2 个需要并排居中的视图。试图优化这一点,所以我想避免嵌套布局,尤其是线性,彼此内部。

Is there something obvious that I'm missing? I thought this attribute is made for this situation.

有什么明显的东西我失踪了吗?我认为这个属性是为这种情况而设计的。

采纳答案by mindriot

I answered a similar issue involving three views without using nested ViewGroups.

我在不使用嵌套 ViewGroup 的情况下回答了涉及三个视图的类似问题。

https://stackoverflow.com/a/13279846/1011746

https://stackoverflow.com/a/13279846/1011746

This is tested in API 11.

这在 API 11 中进行了测试。

For the two view horizontal case:

对于两个视图水平的情况:

<RelativeLayout
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:gravity="center"
  android:background="@android:color/black"
  >
  <Button
    android:id="@+id/apply"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="APPLY"
    android:textSize="20sp"
    />
  <Button
    android:id="@+id/undo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="UNDO"
    android:textSize="20sp"
    android:layout_toRightOf="@id/apply"
    />
</RelativeLayout>

For the two view vertical case:

对于两个视图垂直的情况:

<RelativeLayout
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:gravity="center"
  android:background="@android:color/black"
  >
  <Button
    android:id="@+id/apply"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="APPLY"
    android:textSize="20sp"
    />
  <Button
    android:id="@+id/undo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="UNDO"
    android:textSize="20sp"
    android:layout_below="@id/apply"
    />
</RelativeLayout>

回答by Joakim Berglund

You'll need to nest several layouts together. To center something in a RelativeLayout, you use android:layout_centerInParent="true"on the child. If you try to center several childs, they'll end up under/over each other.

您需要将多个布局嵌套在一起。要在 RelativeLayout 中居中某些内容,您可以android:layout_centerInParent="true"在 child 上使用。如果你试图让几个孩子居中,他们最终会在彼此下方/上方。

Therefore, for example, you could use a LinearLayout with two views as a child to the RelativeLayout, with the LinearLayout having android:orientation="horizontal"and android:layout_centerInParent="true". The LinearLayout should now be centered in the RelativeLayout, with the two children next to each other.

因此,例如,您可以使用带有两个视图的 LinearLayout 作为 RelativeLayout 的子项,而 LinearLayout 具有android:orientation="horizontal"android:layout_centerInParent="true"。LinearLayout 现在应该在相对布局中居中,两个孩子彼此相邻。

回答by Barak

Wrap the two views in a LinearLayout and then center the LinearLayout in the RelativeLayout like you did for the single TextView.

将两个视图包装在一个 LinearLayout 中,然后像在单个 TextView 中所做的那样将 LinearLayout 居中放置在 RelativeLayout 中。

回答by Frank Sposaro

So my fix for this issue turn out just to leverage the compound drawable feature of textview. I just trashed the button and used drawableRight to show the search icon.

所以我对这个问题的修复只是为了利用 textview 的复合可绘制功能。我只是删除了按钮并使用 drawableRight 来显示搜索图标。