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

在配置xml布局时,经常用到 android:gravity   android:layout_gravity这两个属性,这里记录一下他们的区别。


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使用的时候一般都要有上级布局,也就是不能用于根节点,如下代码所示:

<?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:layout_gravity="center"
    android:orientation="vertical">

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

        <EditText
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="input text" />

    </LinearLayout>

</LinearLayout>

红色部分的代码
android:layout_gravity="center"

就是无效代码。

下面布局和效果图:

<?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:orientation="vertical">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:orientation="vertical">

        <EditText
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="input text" />

    </LinearLayout>

</LinearLayout>



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

 

[java]  view plain  copy
 print ?
  1. Button button  = new Button(this);  
  2. button.setText("One");  
  3. LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);  
  4. //此处相当于布局文件中的Android:layout_gravity属性  
  5. lp.gravity = Gravity.RIGHT;  
  6. button.setLayoutParams(lp);  
  7. //此处相当于布局文件中的Android:gravity属性  
  8. button.setGravity(Gravity.CENTER);  
  9.   
  10. LinearLayout linear = new LinearLayout(this);  
  11. //注意,对于LinearLayout布局来说,设置横向还是纵向是必须的!否则就看不到效果了。  
  12. linear.setOrientation(LinearLayout.VERTICAL);  
  13. linear.addView(button);  
  14. setContentView(linear);  
 

 

或者这样也可以:

 

[java]  view plain  copy
 print ?
  1. Button button  = new Button(this);  
  2. button.setText("One");  
  3. //此处相当于布局文件中的Android:gravity属性  
  4. button.setGravity(Gravity.CENTER);  
  5.   
  6. LinearLayout linear = new LinearLayout(this);  
  7. //注意,对于LinearLayout布局来说,设置横向还是纵向是必须的!否则就看不到效果了。  
  8. linear.setOrientation(LinearLayout.VERTICAL);  
  9.   
  10. LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);  
  11. //此处相当于布局文件中的Android:layout_gravity属性  
  12. lp.gravity = Gravity.RIGHT;  
  13.   
  14. linear.addView(button, lp);  
  15. 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);  


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值