android之layout_gravity和gravity的区别

layout_gravity是设置本控件(子控件)相对父类控件所在的位置

gravity:有两个作用

 一:如果是在父类用:则子控件都是按照这个gravity的对齐方式放置

二:如果是用在子控件中,则是控件中文字的对象方式


例子:

<TextView 
            android:id="@+id/tv_activity_program_order"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.3"
            android:gravity="center_horizontal"
            android:layout_marginLeft="@id/tv_activity_program_program"
            android:text=字体在中间"/>  

则“字体在中间”字体是在子控件的中间


 <LinearLayout
        android:id="@+id/linear_activity_program_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:orientation="horizontal" >


       <TextView 
            android:id="@+id/tv_activity_program_order"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.3"
            android:layout_gravity="center_horizontal"
            android:text="tv组件在中间"/>  

则这个textview控件是放在父控件lineatLayout的中间



<LinearLayout
        android:id="@+id/linear_activity_program_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal" >

  <TextView 
            android:id="@+id/tv_activity_program_order"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.3"
            android:text="所有子控件都是在中间"/>  

       <TextView 
            android:id="@+id/tv_activity_program_order"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.3"
            android:text="所有子控件都是在中间"/>  
    </LinearLayout>

则:这两个textview控件都会居中对齐




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值