转自:http://blog.csdn.net/feng88724/article/details/6333809

在进行UI布局的时候,可能经常会用到 android:gravity 和 android:layout_Gravity 这两个属性。

关于这两个属性的区别,网上已经有很多人进行了说明,这边再简单说一下。 (资料来自网络)

LinearLayout有两个非常相似的属性:

android:gravity与android:layout_gravity。

他们的区别在于:

android:gravity 属性是对该view中内容的限定.比如一个button 上面的text. 你可以设置该text 相对于view的靠左,靠右等位置.

android:layout_gravity是用来设置该view相对与父view 的位置.比如一个button 在linearlayout里,你想把该button放在linearlayout里靠左、靠右等位置就可以通过该属性设置.

即android:gravity用于设置View中内容相对于View组件的对齐方式,而android:layout_gravity用于设置View组件相对于Container的对齐方式。

原理跟android:paddingLeft、android:layout_marginLeft有点类似。如果在按钮上同时设置这两个属性。

android:paddingLeft="30px" 按钮上设置的内容离按钮左边边界30个像素
android:layout_marginLeft="30px" 整个按钮离左边设置的内容30个像素


下面回到正题, 我们可以通过设置android:gravity="center"来让EditText中的文字在EditText组件中居中显示;同时我们设置EditText的android:layout_gravity="right"来让EditText组件在LinearLayout中居右显示。看下效果:

Android 中两种方法设置android:gravity 和 android:layout_gravity属性 ._第1张图片

正如我们所看到的,在EditText中,其中的文字已经居中显示了,而EditText组件自己也对齐到了LinearLayout的右侧。

附上布局文件:

[xhtml] view plain copy print ?
  1. <LinearLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <EditText
  7. android:layout_width="wrap_content"
  8. android:gravity="center"
  9. android:layout_height="wrap_content"
  10. android:text="one"
  11. android:layout_gravity="right"/>
  12. </LinearLayout>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <EditText android:layout_width="wrap_content" android:gravity="center" android:layout_height="wrap_content" android:text="one" android:layout_gravity="right"/> </LinearLayout>

那么上面是通过布局文件的方式来设置的。,相信大家都曾写过,那么如何通过Java代码来设置组件的位置呢?

依然考虑实现上述效果。

通过查看SDK,发现有一个setGravity方法, 顾名思义, 这个应该就是用来设置Button组件中文字的对齐方式的方法了。

仔细找了一圈,没有发现setLayoutgravity方法, 有点失望。 不过想想也对, 如果这边有了这个方法, 将Button放在不支持Layout_Gravity属性的Container中如何是好!

于是想到, 这个属性有可能在Layout中 , 于是仔细看了看LinearLayout 的 LayoutParams, 果然有所发现, 里面有一个 gravity 属性,相信这个就是用来设置组件相对于容器本身的位置了,没错,应该就是他了。

实践后发现,如果如此, 附上代码,各位自己看下。

代码比较简单,但是发现它们还是花了我一点时间的。

[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. LinearLayout linear = new LinearLayout(this);
  10. //注意,对于LinearLayout布局来说,设置横向还是纵向是必须的!否则就看不到效果了。
  11. linear.setOrientation(LinearLayout.VERTICAL);
  12. linear.addView(button);
  13. setContentView(linear);

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);

或者这样也可以:

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


如果觉得本文对您有帮助, 还请留言支持一下, 非常感谢!

更多相关文章

  1. android线性布局之比例
  2. android EditText中inputType的属性列表
  3. Android ListView重要美化属性
  4. Android: TextView常用属性的用法详解
  5. android相对布局简介
  6. android常用布局的使用
  7. Android常用布局、控件以及Android存储方式

随机推荐

  1. Mysql主从同步的实现原理
  2. 详解Mysql主从同步配置实战
  3. MySQL优化之缓存优化(续)
  4. MySQL优化之连接优化
  5. MySQL优化之缓存优化
  6. MySQL优化之InnoDB优化
  7. MySQL分区表的局限和限制详解
  8. Mac下忘记Mysql的root用户密码的解决方法
  9. MySQL5.7主从配置实例解析
  10. mysql 5.7.17 winx64安装配置教程