?LinearLayout遵循盒模型(Box Model),即控件或子容器会沿一列或一行堆起,一个接一个的排放。
LinearLayout的属性:
???????? 在配置LinearLayout时,可以控制它的五个方面:方向、填充模型、权重、对齐和内边距。
1.?????? 方向:
设置LinearLayout是表示一行或一列。
android:orientation属性: ?将值设置为horizontal就表示行。将值设置为vertical就表示列。
可以通过LinearLayout来调用setOrientation()方法来改变位置,参数:
HORIZONTAL? ?
VERTICAL
2.?????? 填充模型:
LinearLayout中的所有控件必须指定:android:layout_width和android:layout_height属性。这两个属性有以下三种形式:
a.?????? 具体的大小,如:100px;
b.?????? wrap_content(包含内容),表示控件应该保持原来大小;
c.?????? fill_parent(填充父元素),表示在处理完所有其他控件之后,当前控件应该填满包含它的容器的所有空用空间。
3.?????? 权重
android:layout_weight属性:表示为相应控件分配的空间比例。其默认值为0,
如果一个控件设置为1,另一个为2,那么第二个控件占用的空间是第一个的两倍。
另一种方式是以百分比为单位,使用百分比有下面三个步骤:
a.?????? 将布局中控件的layout_width设置为0;
b.?????? 将控件设置成想要的百分比;
c.?????? 保证所有这些控件的百分比和为100.
4.?????? 重力
通过设置控件android:layout_gravity属性可以设置控件的对齐方式;值:left(左对齐)、center_vertical(水平居中对齐)、right(居右对齐)。
在JAVA可以调用setGravity()来控制控件的位置。
5.?????? 内边距
通过android:padding属性可以为部件的四边设置内边距。
属性:android:paddingLeft(左边距)、android:paddinRight(右边距)、android:paddinTop(上边距)、android:paddinButton(下边距),单位是px,如:5px。
示例:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><RadioGroup android:id="@+id/Horizontal" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5px"> <RadioButton android:id="@+id/rdbHorizontal" android:text="采用水平布局"/> <RadioButton android:id="@+id/rdbVertical" android:text="采用垂直布局"/></RadioGroup><RadioGroup android:id="@+id/rdbGravity" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="5px"> <RadioButton android:id="@+id/rdbLeft" android:text="居左"/> <RadioButton android:id="@+id/rdbCenter" android:text="居中"/> <RadioButton android:id="@+id/rdbRight" android:text="居右"/> </RadioGroup></LinearLayout>
?
package com.lim.layout;import android.app.Activity;import android.os.Bundle;import android.view.Gravity;import android.widget.LinearLayout;import android.widget.RadioGroup;public class layoutAndroid extends Activity implements RadioGroup.OnCheckedChangeListener { private RadioGroup rdgOrientation; private RadioGroup rdgGravity; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); rdgOrientation = (RadioGroup)findViewById(R.id.Horizontal); rdgOrientation.setOnCheckedChangeListener(this); rdgGravity = (RadioGroup)findViewById(R.id.rdbGravity); rdgGravity.setOnCheckedChangeListener(this); } public void onCheckedChanged(RadioGroup group, int checkedId) { switch(checkedId) { case R.id.rdbHorizontal: rdgOrientation.setOrientation(LinearLayout.HORIZONTAL); break; case R.id.rdbVertical: rdgOrientation.setOrientation(LinearLayout.VERTICAL); break; case R.id.rdbLeft: rdgGravity.setGravity(Gravity.LEFT); break; case R.id.rdbCenter: rdgGravity.setGravity(Gravity.CENTER_HORIZONTAL); break; case R.id.rdbRight: rdgGravity.setGravity(Gravity.RIGHT); break; } }}
?
运行效果:
?
??
?