Android中 android:gravity 和 android:layout_gravity的区别

转载  http://blog.csdn.net/jdsjlzx/article/details/50710228

1.android:gravity  


android:gravity 常用于控制view的内部控件或者内容的位置,类似于于padding,如下所示:


<LinearLayout  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:gravity="right"   //控制EditText控件的位置居右  
        android:orientation="vertical">  
  
        <EditText  
            android:layout_width="200dp"  
            android:layout_height="wrap_content"  
            android:gravity="center" //控制EditText控件的文本的位置居中  
            android:text="input text" />  
  
    </LinearLayout>  




下面代码


<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:gravity="center"  
    android:orientation="vertical">  
  
    <LinearLayout  
        <span style="color:#ff0000;">android:layout_width="match_parent"  
        android:layout_height="wrap_content"</span>  
        android:gravity="right"  
        android:orientation="vertical">  
  
        <EditText  
            android:layout_width="200dp"  
            android:layout_height="wrap_content"  
            android:gravity="center"  
            android:text="input text" />  
  
    </LinearLayout>  
  
</LinearLayout>  






在根节点布局中使用了android:gravity="center",影响了里面的view中的位置,不难发现,第二个LinearLayout设置了android:layout_width="match_parent",位置没有受到影响,设置了android:layout_height="wrap_content",第二个LinearLayout就垂直居中了。


2.android:layout_gravity


android:layout_gravity使用的时候一般都要有上级布局,也就是不能用于根节点,如下代码所示:


Java代码来设置组件的位置(参考http://blog.csdn.NET/feng88724)


Button button  = new Button(this);  
button.setText("One");  
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);  
//此处相当于布局文件中的Android:layout_gravity属性  
lp.gravity = Gravity.RIGHT;  
button.setLayoutParams(lp);  
//此处相当于布局文件中的Android:gravity属性  
button.setGravity(Gravity.CENTER);  
  
LinearLayout linear = new LinearLayout(this);  
//注意,对于LinearLayout布局来说,设置横向还是纵向是必须的!否则就看不到效果了。  
linear.setOrientation(LinearLayout.VERTICAL);  
linear.addView(button);  
setContentView(linear);  

Button button  = new Button(this);  
button.setText("One");  
//此处相当于布局文件中的Android:gravity属性  
button.setGravity(Gravity.CENTER);  
  
LinearLayout linear = new LinearLayout(this);  
//注意,对于LinearLayout布局来说,设置横向还是纵向是必须的!否则就看不到效果了。  
linear.setOrientation(LinearLayout.VERTICAL);  
  
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);  
//此处相当于布局文件中的Android:layout_gravity属性  
lp.gravity = Gravity.RIGHT;  
  
linear.addView(button, lp);  
setContentView(linear);  

另外,要设置在RelativeLayout中的位置时使用addRule方法,如下:

[java]  view plain  copy
 print ?
  1. params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  2.         params.addRule(RelativeLayout.CENTER_IN_PARENT);  
  3.         mContainer.addView(progress,params);  


















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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值