layout_gravity在水平线性布局中不起作用 [英] layout_gravity not working in horizontal linear layout

查看:86
本文介绍了layout_gravity在水平线性布局中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在水平linear layout中使用layout_gravity.但这似乎不起作用.我不想不想使用relative layout或其他任何布局.我想知道为什么它不能与linear layout一起使用.我早些时候在vertical线性布局中使用了layout_gravity,并且它以我期望的方式工作.

I have been trying to use layout_gravity in a horizontal linear layout. But it doesn't seem to work. I don't want to use a relative layout or any other layout. I want to know why it is not working with linear layout.I have earlier used layout_gravity in vertical linear layouts and it has worked in the way I expected.

<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的左侧.

As you can see the the linear layout has a orientation=horizontal and width=match_parent(it is just inside the root layout). But both the TextViews are displayed sticking to the left of the LinearLayouteven though I have given their layout_gravity=center and layout_gravity=right respectively.

推荐答案

简短答案:如果您具有水平的LinearLayout,则每个子项只能具有layout_gravity值,分别为topbottomcenter .

Short answer: If you have a horizontal LinearLayout, then each child can only have layout_gravity values of top, bottom, and center.

这样做的原因是因为您已经告诉LinearLayout按照指定的顺序从左到右水平对齐每个子级,这意味着它仅允许您指定每个子级在垂直方向上的对齐方式. layout_gravity参数,而LinearLayout垂直时则相反.

The reason for this is because you've already told the LinearLayout to align each child horizontally, left-to-right in the order you specify, which means that it only lets you specify the vertical alignment of each child in the layout_gravity parameter, and the opposite applies when your LinearLayout is vertical.

此处有一个很好的链接,解释了各种重力如何工作.

There's a good link explaining how the various gravities work here.

这篇关于layout_gravity在水平线性布局中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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