水平线性布局中的layout_gravity属性无法工作

32

我一直在尝试在水平的线性布局中使用layout_gravity,但似乎不起作用。我不想使用相对布局或其他任何布局。我想知道为什么在线性布局中它不能正常工作。我之前在垂直线性布局中使用了layout_gravity,并且按照我预期的方式工作。

<LinearLayout
    android:id="@+id/shata"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <TextView
        android:id="@+id/title_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textColor="@color/black"
        android:text="shata1"
        android:textAppearance="?android:attr/textAppearanceMedium"
        />
    <TextView
        android:id="@+id/map_imageview"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="shata2"/>    
</LinearLayout>

您可以看到,线性布局具有orientation=horizontalwidth=match_parent(它只是在根布局内部)。但是,尽管我已经分别给出了它们的layout_gravity=centerlayout_gravity=right,但两个TextView都显示为粘附在LinearLayout的左侧。


它不会起作用,因为TextView的布局绑定在wrap_content而不是match_parent - ImMathan
线性布局不起作用,你应该使用相对布局。为什么不想使用相对布局呢? - Muzaffer
2
在LinearLayout中,layout_gravity只能改变对象相对于方向的垂直位置。如果方向是水平的,则可以指定顶部和底部,而不能指定左侧和右侧。 - Raghu Teja
@chitti:但我看到layout_gravity=bottom在垂直线性布局中起作用。 - Ashwin
@Ashwin 你可能需要再次检查一下。你可能已经看到了 gravity = bottom 用于线性布局的工作。 - Raghu Teja
6个回答

60

简短回答: 如果您有一个水平的LinearLayout,则每个子元素只能具有layout_gravity值为top, bottom, 和center

这是因为您已经告诉LinearLayout按顺序从左到右横向对齐每个子元素,这意味着它只允许您在layout_gravity参数中指定每个子元素的垂直对齐方式,而当您的LinearLayout是垂直时则相反。

这里有一个很好的链接解释各种重力是如何工作的:here


2
起初,我也是这样认为的。但按照这个逻辑,垂直LinearLayout应该只接受left、right和center的值。但是,垂直布局也会对bottom属性做出反应。 - Ashwin
1
我很想看到一个示例,其中垂直响应“底部”,因为就我所知,它不应该这样做,这可能只是其他子元素分配的权重的产物。 - Matt Taylor
解释比答案更好,这也清楚地反映在投票中:)) - Farid

11
作为Matt Taylor's response的补充,您可以使用layout_weight属性指定布局中元素的大小比例。有关更多详细信息,请参见此thread
上面的代码将把水平空间分成三个相等的区域。
<LinearLayout
    android:id="@+id/shata"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0"
        android:layout_height="match_parent"
        android:layout_weight="1"/>
    <TextView
        android:layout_width="0"
        android:layout_height="match_parent"
        android:layout_weight="1"/>
    <TextView
        android:layout_width="0"
        android:layout_height="match_parent"
        android:layout_weight="1"/>    
</LinearLayout>

因此,在这个例子中

  • 第一个元素看起来像 layout_gravity="left"
  • 第二个元素看起来像 layout_gravity="center"
  • 第三个元素看起来像 layout_gravity="right"

Thomas,你底部TextView的layout_weight缺少闭合双引号(在1后面)。只是提醒一下。 - Alyoshak
非常有限的实用性。因为如果你想在左侧放置两个元素(一个接一个),而在右侧只放置一个元素呢? - ekashking

3
您可以将布局分为三个部分,然后在右侧部分放置“shata2”,在中心部分放置“shata1”,左侧部分放置一个透明视图。
<LinearLayout
    android:id="@+id/shata"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        />

    <TextView
        android:id="@+id/title_textview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center|top"
        android:layout_weight="1"
        android:text="shata1"
        android:gravity="center"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#111" />

    <TextView
        android:id="@+id/map_imageview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="right"
        android:gravity="right"
        android:text="shata2"
        android:textAppearance="?android:attr/textAppearanceMedium" />



</LinearLayout>

但是在你的代码中,当你使用线性布局时,它只是将视图一个接一个地放置,而不关心布局重力。 希望这对你有用。


2

如果您想让您的孩子居中,那么您只需要为子元素给予线性布局重力。在您的线性布局上设置以下内容:

android:gravity="center"

我希望我理解了你的问题。

0
创建一个水平线性布局中的三个子项的一种方法(已测试)可能是:
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:weightSum="3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_gravity="center_horizontal"
            android:layout_height="wrap_content"/>

            <TextView
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="" />

-2
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<TextView
    style="@style/Base.TextAppearance.AppCompat.Headline"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Blankets &amp;  Carpets" />

<ListView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/list_home_feature"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="30dp"
    android:layout_marginRight="10dp">

</ListView>

</LinearLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接