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

或者这样也可以:

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

好了,效果图就不上了,跟上面的一样。 就讲这么多。

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

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


更多相关文章

  1. Android(安卓)Studio App设置线性布局LinerLayout控件占屏幕长宽
  2. Android(安卓)如何将一个Activity设置成窗口样式
  3. Android之drawable state各个属性详解
  4. Android中gravity与layout_gravity的区别
  5. android:windowBackground 和 android:background 的区别
  6. android软键盘设置
  7. android 布局大全
  8. android:windowSoftInputMode属性使用
  9. android TextView属性详解

随机推荐

  1. AIOps 如何优雅服务应用运维?看民生银行智
  2. 分表分库后的id分配问题
  3. 关于面试中必问的跨表Join问题
  4. 这个数据分析师今年升职加薪了!他做对了什
  5. 0318前端第一课作业vscode安装及插件使用
  6. Emmet 语法规则
  7. 如何安装vscode,以及markdown语法的使用
  8. markdown 语法及Emmet 插件的使用
  9. vs code,md,emmet语法
  10. Markdown基本语法