我知道我们可以为android设置以下值:gravity和android:layout_gravity财产:
居中中心_垂直center_horizontal等。
但我对这两者都感到困惑。
android:gravity和android:layout_gravity的用法有什么区别?
我知道我们可以为android设置以下值:gravity和android:layout_gravity财产:
居中中心_垂直center_horizontal等。
但我对这两者都感到困惑。
android:gravity和android:layout_gravity的用法有什么区别?
当前回答
不同之处在于
android:layout_gravity是视图的外部重力。指定视图应接触其父对象边界的方向。
android:重力是该视图的内部重力。指定其内容应对齐的方向。
HTML/CSS等价物
(如果您来自web开发背景)
Android | CSS
————————————————————————+————————————
android:layout_gravity | float
android:gravity | text-align
帮助你记住的简单技巧
将布局重力作为“外部重力”。
其他回答
android:gravity
用于调整视图相对于其指定位置(分配区域)的内容。如果layout_width等于“wrap_content”,android:gravity=“left”将不会执行任何操作
android:layout_gravity
用于相对于父文件或布局文件的视图本身。
android:gravity用于指定如何将对象的内容放置在对象本身中。换句话说,android:gravity用于指定视图内容的重力。
android:layoutgravity是孩子可以提供给父母的属性,用于指定父母内部视图的重力。
有关详细信息,请访问
http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html
重力:允许您移动容器内的内容。(如何放置子视图)。
重要提示:(在可用空间内沿X轴或Y轴移动)。
示例:假设您要使用LinearLayout(Height:match_parent,Width:match_parent)作为根级元素,那么您将拥有完整的帧空间;并且子视图表示LinearLayout内的2个文本视图(Height:wrap_content,Width:wrap-content)可以使用父视图上的重力值沿x/y轴移动。
Layout_Gravity:允许您仅沿x轴覆盖父重力行为。
重要提示:(在可用空间内沿X轴移动[覆盖])。
示例:如果您记住前面的示例,我们知道重力使我们能够沿着x/y轴移动,即:;将TextViews放置在LinearLayout中。让我们假设LinearLayout指定重力:中心;这意味着每个TextView都需要垂直和水平居中。现在,如果我们希望其中一个TextView向左/向右移动,我们可以使用TextView上的layout_gravity覆盖指定的重力行为。
另外:如果你深入挖掘,你会发现TextView中的文本充当子视图;因此,如果在TextView上应用重力,TextView内的文本将四处移动。(整个概念也适用于此)
不同之处在于
android:layout_gravity是视图的外部重力。指定视图应接触其父对象边界的方向。
android:重力是该视图的内部重力。指定其内容应对齐的方向。
HTML/CSS等价物
(如果您来自web开发背景)
Android | CSS
————————————————————————+————————————
android:layout_gravity | float
android:gravity | text-align
帮助你记住的简单技巧
将布局重力作为“外部重力”。
重力和布局重力有很多不同。我将解释我关于这两个概念的经验(我的观察和一些网站获得的所有信息)。
在FrameLayout中使用重力和布局重力。。。。。
注:-
重力在“视图内容”中使用,因为某些用户有答案,并且对于所有“视图组布局”也是如此。布局重力与父视图一起使用,因为某些用户有答案。重力和布局重力对于FrameLayout子对象更有用。我们不能在FrameLayout的标记中使用重力和布局重力。。。。我们可以使用布局重力在FrameLayout中的任何位置设置“子视图”。我们可以使用FrameLayout中的每一个重力值(例如:-center_vertical、center_horizontal、center、top等),但其他ViewGroup Layout无法使用。FrameLayout完全适用于布局重力。示例:-如果您使用FrameLayout,则无需更改整个布局以添加新视图。您只需将视图添加为FrameLayout中的最后一个,并为其提供具有值的布局重力。(这是FrameLayout布局重力的优势)。
看看例子。。。。。。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:textSize="25dp"
android:background="#000"
android:textColor="#264bd1"
android:gravity="center"
android:layout_gravity="center"
android:text="Center Layout Gravity"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:textColor="#1b64b9"
android:gravity="bottom"
android:layout_gravity="bottom|center"
android:text="Bottom Layout Gravity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:textColor="#d75d1c"
android:gravity="top"
android:layout_gravity="top|center"
android:text="Top Layout Gravity"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginTop="100dp"
android:textColor="#d71f1c"
android:gravity="top|right"
android:layout_gravity="top|right"
android:text="Top Right Layout Gravity"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginBottom="100dp"
android:textColor="#d71cb2"
android:layout_gravity="bottom"
android:gravity="bottom"
android:text="Top Left Layout Gravity"/>
</FrameLayout>
输出:-
LinearLayout中重力和布局重力的使用。。。。。
重力的工作方式与上述相同,但这里的不同之处在于,我们可以在LinearLayout视图和RelativeLayout视图中使用重力,这在FrameLayout视图中是不可能的。
方向垂直的LinearLayout。。。。
注意:这里我们只能设置layout_gravity的三个值,即(左|右|中(也称为center_horizontal))。
查看示例:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:textSize="25dp"
android:background="#000"
android:textColor="#264bd1"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:text="Center Layout Gravity \nor \nCenter_Horizontal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginTop="20dp"
android:textColor="#d75d1c"
android:layout_gravity="right"
android:text="Right Layout Gravity"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginBottom="100dp"
android:textColor="#d71cb2"
android:layout_gravity="left"
android:layout_marginTop="20dp"
android:gravity="bottom"
android:text="Left Layout Gravity"/>
</LinearLayout>
输出:-
方向为水平的LinearLayout。。。。
注意:这里我们还可以设置layout_gravity的三个值,即(top|bottom|center(也称为center_vertical))。
查看示例:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<TextView
android:layout_width="120dp"
android:layout_height="100dp"
android:textSize="25dp"
android:background="#000"
android:textColor="#264bd1"
android:gravity="center"
android:layout_gravity="bottom"
android:text="Bottom \nLayout \nGravity"/>
<TextView
android:layout_width="120dp"
android:layout_height="100dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginTop="20dp"
android:textColor="#d75d1c"
android:layout_gravity="center"
android:text="Center \nLayout \nGravity"/>
<TextView
android:layout_width="150dp"
android:layout_height="100dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginBottom="100dp"
android:textColor="#d71cb2"
android:layout_gravity="left"
android:layout_marginTop="20dp"
android:text="Left \nLayout \nGravity"/>
</LinearLayout>
输出:-
注意:-我们不能在RelativeLayout视图中使用layout_gravity,但我们可以使用重力将RelativeLlayout子对象设置为相同的位置。。。。