一个关于gravity和layout_gravity的例子

——处变不惊


这其实是两部分,分开一些比较清晰

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#666666">
    <!-- 
        TextView 一只
        LinearLayout 一只(包含三只按钮)
    -->

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Linear Layout - horizontal, gravity=center"
        android:textColor="#FFFFFF"
        android:padding="2dip" />
    <LinearLayout
        android:layout_width="fill_parent"
        android:orientation="horizontal"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#EEEEEE">

        <!-- LinearLayout  gravity="center" 
                Button中 layout_gravity
        -->

        <Button
            android:id="@+id/Button01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:text="top">

        </Button>

        <Button
            android:id="@+id/Button02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="center">

        </Button>
        <Button
            android:id="@+id/Button03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="bottom"
            android:layout_gravity="bottom">

        </Button>
    </LinearLayout>
</LinearLayout>

图片放中间(图是盗的,自己在环境中尝试一下吧)
http://sandipchitale.blogspot.co.uk/2010/05/linearlayout-gravity-and-layoutgravity.html
这里写图片描述

<!-- 
    TextView 一只
    LinearLayout 一只(包含三只按钮)
    -->

 <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Linear Layout - vertical, gravity=center"
        android:textColor="#FFFFFF"
        android:padding="2dip" />

    <LinearLayout 
        android:layout_width="fill_parent"
        android:orientation="vertical"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#DDDDDD">

        <Button
            android:id="@+id/Button04"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:text="left">

        </Button>
        <Button
            android:id="@+id/Button05"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="center">

        </Button>
        <Button
            android:id="@+id/Button06"
            android:layout_height="wrap_content"
            android:text="right"
            android:layout_gravity="right"
            android:layout_width="wrap_content">
        </Button>

    </LinearLayout>
</LinearLayout>

总结也搬来了,英文的

android:gravity attribute

Strictly speaking android:gravity is not a Layout Param. The android:gravity is really an attribute of the View Group. It controls the way the contents of the View Group will be positioned horizontally and vertically.

  1. In the above example the top Linear Layout is a horizontal Linear Layout. So setting the android:gravity=center has an effect of positioning the set of buttons in the horizontal center.

  2. In the above example the bottom Linear Layout is a vertical Linear Layout. So setting the android:gravity=center has an effect of positioning the set of buttons in the vertical center.

android:layout_gravity Layout Param

The android:layout_gravity is a Layout Param.

Not all View Groups support this Layout Param. See the documentation to find out which Layout Params are supported by a particular View Group. Linear Layout does support android:layout_gravity Layout Param.

For a horizontal Linear Layout the following values make sense:

  • top
  • center
  • bottom

That is because the children of a horizontal Linear Layout are laid out horizontally one after the other. Only thing can be controlled using the android:layout_gravity is how a child view is positioned vertically.

同样,for a vertical Linear Layout the following values make sense:

  • left
  • center
  • right
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值