为什么重力在应用于相对布局时不起作用? [英] Why doesn't gravity work when applied to Relative Layout?

查看:28
本文介绍了为什么重力在应用于相对布局时不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经明白,如果我在文本视图上使用 layout_gravity="center_horizo​​ntal",那么文本将居中.但我不想使用它,因为我希望代码尽可能少,最好的方法是将 gravity="center_horizo​​ntal" 应用到我的相对布局.我也在问这个问题,因为我担心甚至将 gravitylayout_gravity 与相对布局一起使用.在做研究时,我发现了

解决方案

这是对

如您所见,gravity 有效,但 layout_gravity 无效.

具有gravity

的相对布局

我的

我调整了所有子视图的宽度,以便更清楚地了解正在发生的事情.请注意,RelativeLayout 处理重力的方式是将所有子视图作为一个组并在布局中移动它们.这意味着最宽的视图将决定其他一切的位置.因此,相对布局中的重力可能仅在所有子视图具有相同宽度时才有用.

具有gravity

的线性布局

当您将 gravity 添加到 LinearLayout 时,它确实按预期排列子视图.例如,可以保存代码"通过将 LinearLayoutgravity 设置为 center_horizo​​ntally.这样就不需要单独设置每个子视图的 layout_gravity.请参阅下图中的各种选项.

请注意,当视图使用 layout_gravity 时,它会覆盖 LinearLayout 的重力.(这可以在左图中两个布局的标题中看到.LinearLayout gravity 被设置为 leftright,但是标题 TextView 的 layout_gravity 设置为 center_horizo​​ntally.)

最后的笔记

在 RelativeLayout 中定位视图时,一般的做法是在每个视图中添加如下内容:

  • layout_alignParentTop
  • layout_centerVertical
  • layout_below
  • layout_toRightOf

如果想一次设置所有视图,LinearLayout 可能会更好(或者使用 Style).

总结一下,

  • layout_gravity 不适用于 RelativeLayout 中的子视图.
  • RelativeLayout 的 gravity 确实有效,但并不像人们预期的那样.

补充 XML

图像的 XML 在相对布局中查看 gravitylayout_gravity":

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android";android:layout_width=match_parent"android:layout_height=match_parent"机器人:方向=垂直"><相对布局android:layout_width=match_parent"android:layout_height=0dp"android:layout_weight=1"机器人:背景=#e3e2ad"><文本视图android:id="@+id/tvTop1";android:layout_width="wrap_content";android:layout_height="wrap_content";android:layout_centerHorizo​​ntal="true"android:textSize="24sp";android:text=视图的重力="/><!-- 重力的例子--><文本视图android:id="@+id/tvTop2"​​;android:layout_below="@id/tvTop1";android:layout_width=200dp"android:layout_height=40dp"机器人:背景=#bcf5b1";机器人:重力=左"机器人:文本=左"/><文本视图android:id="@+id/tvTop3";android:layout_below=@id/tvTop2"android:layout_width=200dp"android:layout_height=40dp"机器人:背景=#aacaff";android:gravity="center_horizo​​ntal";android:text="center_horizo​​ntal";/><文本视图android:id="@+id/tvTop4";android:layout_below="@id/tvTop3";android:layout_width=200dp"android:layout_height=40dp"机器人:背景=#bcf5b1";机器人:重力=正确"机器人:文本=正确"/><文本视图android:id="@+id/tvTop5";android:layout_below=@id/tvTop4"android:layout_width=200dp"android:layout_height=40dp"机器人:背景=#aacaff";机器人:重力=中心"机器人:文本=中心"/></RelativeLayout><相对布局android:layout_width=match_parent"android:layout_height=0dp"android:layout_weight=1"机器人:背景=#d6c6cd"><文本视图android:id="@+id/tvBottom1";android:layout_width="wrap_content";android:layout_height="wrap_content";android:layout_centerHorizo​​ntal="true"android:textSize="24sp";android:text="Views' layout_gravity="/><!-- layout_gravity 的例子--><文本视图android:id="@+id/tvBottom2";android:layout_below="@id/tvBottom1";android:layout_width=200dp"android:layout_height=40dp"android:layout_gravity=左"机器人:背景=#bcf5b1";机器人:文本=左"/><文本视图android:id="@+id/tvBottom3";android:layout_below="@id/tvBottom2";android:layout_width=200dp"android:layout_height=40dp"android:layout_gravity=center_horizo​​ntal"机器人:背景=#aacaff";android:text="center_horizo​​ntal";/><文本视图android:id="@+id/tvBottom4";android:layout_below=@id/tvBottom3"android:layout_width=200dp"android:layout_height=40dp"android:layout_gravity=正确"机器人:背景=#bcf5b1";机器人:文本=正确"/><文本视图android:id="@+id/tvBottom5";android:layout_below=@id/tvBottom4"android:layout_width=200dp"android:layout_height=40dp"android:layout_gravity=中心"机器人:背景=#aacaff";机器人:文本=中心"/></RelativeLayout></LinearLayout>

图像的 XML 具有重力的相对布局":

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android";android:layout_width=match_parent"android:layout_height=match_parent"机器人:方向=垂直"><相对布局android:layout_width=match_parent"android:layout_height=0dp"android:layout_weight=1"android:gravity="center_horizo​​ntal";机器人:背景=#e3e2ad"><文本视图android:id="@+id/tvTop1";android:layout_width="wrap_content";android:layout_height="wrap_content";android:textSize="24sp";android:text=视图的重力="/><!-- 重力的例子--><文本视图android:id="@+id/tvTop2"​​;android:layout_below="@id/tvTop1";android:layout_width=200dp"android:layout_height=40dp"机器人:背景=#bcf5b1";机器人:重力=左"机器人:文本=左"/><文本视图android:id="@+id/tvTop3";android:layout_below=@id/tvTop2"android:layout_width=300dp"android:layout_height=40dp"机器人:背景=#aacaff";android:gravity="center_horizo​​ntal";android:text="center_horizo​​ntal";/><文本视图android:id="@+id/tvTop4";android:layout_below="@id/tvTop3";android:layout_width=100dp"android:layout_height=40dp"机器人:背景=#bcf5b1";机器人:重力=正确"机器人:文本=正确"/><文本视图android:id="@+id/tvTop5";android:layout_below=@id/tvTop4"android:layout_width=150dp"android:layout_height=40dp"机器人:背景=#aacaff";机器人:重力=中心"机器人:文本=中心"/></RelativeLayout><相对布局android:layout_width=match_parent"android:layout_height=0dp"android:layout_weight=1"机器人:重力=中心"机器人:背景=#d6c6cd"><文本视图android:id="@+id/tvBottom1";android:layout_width="wrap_content";android:layout_height="wrap_content";android:textSize="24sp";android:text=视图的重力="/><!-- layout_gravity 的例子--><文本视图android:id="@+id/tvBottom2";android:layout_below="@id/tvBottom1";android:layout_width=200dp"android:layout_height=40dp"机器人:重力=左"机器人:背景=#bcf5b1";机器人:文本=左"/><文本视图android:id="@+id/tvBottom3";android:layout_below="@id/tvBottom2";android:layout_width=300dp"android:layout_height=40dp"android:gravity="center_horizo​​ntal";机器人:背景=#aacaff";android:text="center_horizo​​ntal";/><文本视图android:id="@+id/tvBottom4";android:layout_below=@id/tvBottom3"android:layout_width=100dp"android:layout_height=40dp"机器人:重力=正确"机器人:背景=#bcf5b1";机器人:文本=正确"/><文本视图android:id="@+id/tvBottom5";android:layout_below=@id/tvBottom4"android:layout_width=150dp"android:layout_height=40dp"机器人:重力=中心"机器人:背景=#aacaff";机器人:文本=中心"/></RelativeLayout></LinearLayout>

图像的 XML 具有重力的线性布局":

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android";android:layout_width=match_parent"android:layout_height=match_parent"机器人:方向=垂直"><线性布局android:layout_width=match_parent"android:layout_height=0dp"android:layout_weight=1"android:gravity="center_horizo​​ntal";机器人:背景=#e3e2ad"机器人:方向=垂直"><文本视图android:layout_width="wrap_content";android:layout_height="wrap_content";android:layout_gravity=center_horizo​​ntal"android:textSize="24sp";android:text=视图的重力="/><文本视图android:layout_width=200dp"android:layout_height=40dp"机器人:背景=#bcf5b1";机器人:重力=左"机器人:文本=左"/><文本视图android:layout_width=300dp"android:layout_height=40dp"机器人:背景=#aacaff";android:gravity="center_horizo​​ntal";android:text="center_horizo​​ntal";/><文本视图android:layout_width=100dp"android:layout_height=40dp"机器人:背景=#bcf5b1";机器人:重力=正确"机器人:文本=正确"/><文本视图android:layout_width=150dp"android:layout_height=40dp"机器人:背景=#aacaff";机器人:重力=中心"机器人:文本=中心"/></LinearLayout><线性布局android:layout_width=match_parent"android:layout_height=0dp"android:layout_weight=1"机器人:重力=中心"机器人:背景=#d6c6cd"机器人:方向=垂直"><文本视图android:layout_width="wrap_content";android:layout_height="wrap_content";android:layout_gravity=center_horizo​​ntal"android:textSize="24sp";android:text=视图的重力="/><文本视图android:layout_width=200dp"android:layout_height=40dp"机器人:背景=#bcf5b1";机器人:重力=左"机器人:文本=左"/><文本视图android:layout_width=300dp"android:layout_height=40dp"机器人:背景=#aacaff";android:gravity="center_horizo​​ntal";android:text="center_horizo​​ntal";/><文本视图android:layout_width=100dp"android:layout_height=40dp"机器人:背景=#bcf5b1";机器人:重力=正确"机器人:文本=正确"/><文本视图android:layout_width=150dp"android:layout_height=40dp"机器人:背景=#aacaff";机器人:重力=中心"机器人:文本=中心"/></LinearLayout></LinearLayout>

I already understand that if I use layout_gravity="center_horizontal" on the text view, then the text will be center. But I don't want to use that because I want to have as little code as possible and the best way to do that is by applying gravity="center_horizontal" to my relative layout. I am also asking this question because I am concerned about even using gravity or layout_gravity with relative layouts at all. As when doing my research I came upon this answer.

Notice the part that says:

Don't use gravity/layout_gravity with a RelativeLayout. Use them for Views in LinearLayouts and FrameLayouts.

Even though it seems pretty apparent to me from the relative layout Android Documentation, which clearly lists gravity as a valid attribute, that Google intended these attributes to be used with relative layouts.

If that quote is correct, how do I center views in relative layouts?

Also, here is my code:

Notice the title is not centered horizontally

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:gravity="center_horizontal"
    tools:context="com.something">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="@style/TextAppearance.AppCompat.Title"
        android:textAlignment="center"
        android:text="@string/app_title"
        android:layout_marginBottom="@dimen/main_spacing"
        android:textSize="24sp" />

    <AutoCompleteTextView
        android:id="@+id/enterContact"
        android:text="Enter Contact"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:layout_below="@+id/title"
        android:layout_marginBottom="@dimen/main_spacing" />

</RelativeLayout>

解决方案

This is a supplemental answer to Gravity and layout_gravity on Android.

View gravity and layout_gravity inside a Relative Layout

My original statement that gravity and layout_gravity should not be used in the subviews of a RelativeLayout was partly wrong. The gravity actually works fine. However, layout_gravity has no effect. This can be seen in the example below. The light green and light blue are TextViews. The other two background colors are RelativeLayouts.

As you can see, gravity works, but layout_gravity doesn't work.

Relative Layout with gravity

My original answer about gravity and layout_gravity dealt with these attributes being applied to the Views within a ViewGroup (specifically a LinearLayout), not to the ViewGroup itself. However, it is possible to set gravity and layout_gravity on a RelativeLayout. The layout_gravity would affect how the RelativeLayout is positioned within its own parent, so I will not deal with that here. How gravity affects the subviews, though, is shown in the image below.

I resized the widths of all the subviews so that what is happening is more clear. Note that the way RelativeLayout handles gravity is to take all the subviews as a group and move them around the layout. This means that whichever view is widest will determine how everything else is positioned. So gravity in a Relative layout is probably only useful if all the subviews have the same width.

Linear Layout with gravity

When you add gravity to a LinearLayout, it does arrange the subviews as one would expect. For example, one could "save code" by setting the gravity of LinearLayout to center_horizontally. That way there is no need individually set the layout_gravity of each subview. See the various options in the image below.

Note that when a view uses layout_gravity, it overrides the LinearLayout's gravity. (This can be seen in the title for the two layouts in the left image. The LinearLayout gravity was set to left and right, but the title TextView's layout_gravity was set to center_horizontally.)

Final notes

When positioning views within a RelativeLayout, the general way to do it is adding things like the following to each view:

  • layout_alignParentTop
  • layout_centerVertical
  • layout_below
  • layout_toRightOf

If one wants to set all the views at once, a LinearLayout would likely be better (or perhaps using a Style).

So to sum up,

  • The layout_gravity does not work for subviews in a RelativeLayout.
  • The gravity of a RelativeLayout does work, but not as one might expect.

Supplemental XML

XML for image "View gravity and layout_gravity inside a Relative Layout":

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#e3e2ad" >

        <TextView
            android:id="@+id/tvTop1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:textSize="24sp"
            android:text="Views' gravity=" />

        <!-- examples of gravity -->
        
        <TextView
            android:id="@+id/tvTop2"
            android:layout_below="@id/tvTop1"
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:background="#bcf5b1"
            android:gravity="left"
            android:text="left" />

        <TextView
            android:id="@+id/tvTop3"
            android:layout_below="@id/tvTop2"
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:background="#aacaff"
            android:gravity="center_horizontal"
            android:text="center_horizontal" />

        <TextView
            android:id="@+id/tvTop4"
            android:layout_below="@id/tvTop3"
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:background="#bcf5b1"
            android:gravity="right"
            android:text="right" />

        <TextView
            android:id="@+id/tvTop5"
            android:layout_below="@id/tvTop4"
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:background="#aacaff"
            android:gravity="center"
            android:text="center" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#d6c6cd" >

        <TextView
            android:id="@+id/tvBottom1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:textSize="24sp"
            android:text="Views' layout_gravity=" />

        <!-- examples of layout_gravity -->
        
        <TextView
            android:id="@+id/tvBottom2"
            android:layout_below="@id/tvBottom1"
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:layout_gravity="left"
            android:background="#bcf5b1"
            android:text="left" />

        <TextView
            android:id="@+id/tvBottom3"
            android:layout_below="@id/tvBottom2"
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:layout_gravity="center_horizontal"
            android:background="#aacaff"
            android:text="center_horizontal" />

        <TextView
            android:id="@+id/tvBottom4"
            android:layout_below="@id/tvBottom3"
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:layout_gravity="right"
            android:background="#bcf5b1"
            android:text="right" />

        <TextView
            android:id="@+id/tvBottom5"
            android:layout_below="@id/tvBottom4"
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:layout_gravity="center"
            android:background="#aacaff"
            android:text="center" />

    </RelativeLayout>

</LinearLayout>

XML for image "Relative Layout with gravity":

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center_horizontal"
        android:background="#e3e2ad" >

        <TextView
            android:id="@+id/tvTop1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:text="Views' gravity=" />

        <!-- examples of gravity -->

        <TextView
            android:id="@+id/tvTop2"
            android:layout_below="@id/tvTop1"
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:background="#bcf5b1"
            android:gravity="left"
            android:text="left" />

        <TextView
            android:id="@+id/tvTop3"
            android:layout_below="@id/tvTop2"
            android:layout_width="300dp"
            android:layout_height="40dp"
            android:background="#aacaff"
            android:gravity="center_horizontal"
            android:text="center_horizontal" />

        <TextView
            android:id="@+id/tvTop4"
            android:layout_below="@id/tvTop3"
            android:layout_width="100dp"
            android:layout_height="40dp"
            android:background="#bcf5b1"
            android:gravity="right"
            android:text="right" />

        <TextView
            android:id="@+id/tvTop5"
            android:layout_below="@id/tvTop4"
            android:layout_width="150dp"
            android:layout_height="40dp"
            android:background="#aacaff"
            android:gravity="center"
            android:text="center" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#d6c6cd" >

        <TextView
            android:id="@+id/tvBottom1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:text="Views' gravity=" />

        <!-- examples of layout_gravity -->

        <TextView
            android:id="@+id/tvBottom2"
            android:layout_below="@id/tvBottom1"
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:gravity="left"
            android:background="#bcf5b1"
            android:text="left" />

        <TextView
            android:id="@+id/tvBottom3"
            android:layout_below="@id/tvBottom2"
            android:layout_width="300dp"
            android:layout_height="40dp"
            android:gravity="center_horizontal"
            android:background="#aacaff"
            android:text="center_horizontal" />

        <TextView
            android:id="@+id/tvBottom4"
            android:layout_below="@id/tvBottom3"
            android:layout_width="100dp"
            android:layout_height="40dp"
            android:gravity="right"
            android:background="#bcf5b1"
            android:text="right" />

        <TextView
            android:id="@+id/tvBottom5"
            android:layout_below="@id/tvBottom4"
            android:layout_width="150dp"
            android:layout_height="40dp"
            android:gravity="center"
            android:background="#aacaff"
            android:text="center" />

    </RelativeLayout>

</LinearLayout>

XML for image "Linear Layout with gravity":

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center_horizontal"
        android:background="#e3e2ad"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:textSize="24sp"
            android:text="View's gravity=" />

        <TextView
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:background="#bcf5b1"
            android:gravity="left"
            android:text="left" />

        <TextView
            android:layout_width="300dp"
            android:layout_height="40dp"
            android:background="#aacaff"
            android:gravity="center_horizontal"
            android:text="center_horizontal" />

        <TextView
            android:layout_width="100dp"
            android:layout_height="40dp"
            android:background="#bcf5b1"
            android:gravity="right"
            android:text="right" />

        <TextView
            android:layout_width="150dp"
            android:layout_height="40dp"
            android:background="#aacaff"
            android:gravity="center"
            android:text="center" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#d6c6cd"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:textSize="24sp"
            android:text="View's gravity=" />

        <TextView
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:background="#bcf5b1"
            android:gravity="left"
            android:text="left" />

        <TextView
            android:layout_width="300dp"
            android:layout_height="40dp"
            android:background="#aacaff"
            android:gravity="center_horizontal"
            android:text="center_horizontal" />

        <TextView
            android:layout_width="100dp"
            android:layout_height="40dp"
            android:background="#bcf5b1"
            android:gravity="right"
            android:text="right" />

        <TextView
            android:layout_width="150dp"
            android:layout_height="40dp"
            android:background="#aacaff"
            android:gravity="center"
            android:text="center" />

    </LinearLayout>

</LinearLayout>

这篇关于为什么重力在应用于相对布局时不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆