RelativeLayout gravity center not working

只愿长相守 提交于 2019-12-08 15:00:53

问题


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

<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 centerInParent to true for 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.

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


回答1:


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

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

This is tested in 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>



回答2:


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.

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.




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/10904864/relativelayout-gravity-center-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!