layout_gravity和gravity区别

layout_gravity 是控制组件在父组件中的位置
gravity 是控制组件的子组件/内容(文字)在组件中的位置


主要是注意 layout_gravity 在 LinearLayout(线性布局)中的应用
与线性布局的方向相关 对应属性 android:orientation="vertical|horizontal"
1.vertical 横向属性起作用 例如 left right center_horizontal
2.horizontal 纵向属性起作用 例如 top bottom center_vertical
注意 center 属性在俩种情况下都起作用


    <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#f00">


        <TextView
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:background="#0f0"
                android:text="Hello World"
                android:gravity="center"
                android:layout_gravity="right"
                />


    </LinearLayout>
    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:background="#00f">


        <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#0f0"
                android:text="Hello World"
                android:gravity="center"
                android:layout_gravity="bottom"
                />


    </LinearLayout>


在 FrameLayout(帧布局)可以叠加使用 例如 左上 - left|top


    <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="左上"
                android:layout_gravity="left|top"/>


    </FrameLayout>


源代码下载地址




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值