1.线性布局(Linear Layout)(最多(尽量少用相对我们相对布局而言))
是一个ViewGroup以线性方向显示它的子视图(view)元素,即垂直地或水平。
LinearLayout视图组(ViewGroup)包含5个Button,它的子元素是以线性方式(horizontal,水平的)布局。
如果你在android:orientation="horizontal"设置为vertical,则是垂直或者说是纵向的
a.horizontal水平 在同一行内多个子View从左(从viewgroup左上角)往右排列(子View超过屏幕的宽度也不会换行)
b.vertical垂直 从上到下进行排列(从viewgroup左上角开始)
补充:关于权重android:layout_weight="1"
当我们给子View设置权重以后,它会对自身的大小再多计算一次(也就是一共计算两次)
fill_parent和match_parent:父容器viewgroup 有多宽(高),它就占多宽(高)。分配大小时尽可能大
wrap_content:子控件自身的内容有有多大就占多大,分配大小时尽可能小。
weight的计算规则:(计算两次:1.先按照指定的大小排版,2.排版完以后把剩下的空间按比例进行划分再加上之前第一次的大小得到最终的结果)
button1 权重为1 ; button2 权重2 以水平布局为例:
假设屏幕的宽为L
情况1:android:layout_width="wrap_content"
计算过程:
第一次计算
button1 宽为0 button2 宽为0
第二次计算: 剩下空间宽为:L
button1 宽为L/3 button2 宽为L*2/3
那么最后的结果:
button1 宽为L/3 button2 宽为L*2/3 也就是我们看到1:2
情况2:android:layout_width="fill_parent|match_parent"
计算过程:
第一次计算
button1 宽为L button2 宽为L
第二次计算: 剩下空间宽为: -L
button1 宽为-L/3 button2 宽为-L*2/3
那么最后的结果:
button1 宽为L*2/3 button2 宽为L/3 也就是我们看到2:1
2.相对布局(Relative Layout)
以相对位置显示它的子视图元素,一个视图可以指定相对于它的兄弟视图的位置(例如在给定视图的左边或者下面)或相对于特定区域的位置(例如底部对齐,或中间偏左)。相对布局是设计用户界面的有力工具
关键是一些布局的属性的掌握
android:layout_below 相对在某控件下方
android:layout_alignParentRight 相对父容器右对齐
android:layout_marginLeft
android:layout_toLeftOf 相对在某控件左侧
3.帧布局(FrameLayout)(最简单)
从屏幕的原点(左上角)开始布局,多个组件层叠,后面的组件覆盖前面的组件。
4.表格布局(Table Layout)
以表格显示它的子视图元素,即行和列标识一个视图的位置。表格布局中每一行为一个TableRow对象,当然也可以是一个View对象。TableRow可以添加子控件,每添加个为一列。
关键属性:android:shrinkColumns
对应的方法:setShrinkAllColumns(boolean)
作用:设置表格的列是否收缩(列编号从0开始,下同),多列用逗号隔开(下同)
如:android:shrinkColumns="0,1,2",即表格的第1、2、3列的内容是收缩的以适合屏幕,不会挤出屏幕。
android:stretchColumns
对应的方法:setStretchAllColumns(boolean)
作用:设置表格的列是否拉伸
5.绝对布局(AbsoluteLayout)
以绝对方式显示它的子视图(view)元素,即以坐标的方式来定位在屏幕上位置。这种布局方式很好理解,在布局文件或编程里设置View的坐标,从而绝对地定位。
另外,除了利用xml布局实现的静态布局外,还可以使用动态编码
动态编码:其实就是把我们对应的xml文件的布局属性用java代码“翻译”出来
下面以一个简单的demo说明xml文件和java编码实现的布局<?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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入电话号码" />
<EditText
android:id="@+id/et_phonenum"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入电话号码">
<requestFocus />
</EditText>
<Button
android:id="@+id/main_bt_call"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="拨打电话" />
</LinearLayout>
public class MainActivity extends Activity {
private Button bt_call;//拨打电话按钮
private EditText et_phoneNum;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt_call = (Button) findViewById(R.id.main_bt_call);
et_phoneNum = (EditText) findViewById(R.id.et_phonenum);
bt_call.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String phone = et_phoneNum.getEditableText().toString();
Log.e("phone", phone);
Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + phone));
startActivity(intent);
}
});
}
}
动态:
public class MainActivity_Java extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(initView());
}
private LinearLayout initView() {
//根布局
LinearLayout screen = new LinearLayout(MainActivity_Java.this);
//设置布局参数
screen.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
//设置布局样式(水平还是垂直)
screen.setOrientation(LinearLayout.VERTICAL);
//准备添加子view
TextView tv = new TextView(MainActivity_Java.this);
tv.setText("请输入电话号码");
screen.addView(tv);
final EditText et_phoneNum = new EditText(MainActivity_Java.this);
et_phoneNum.setHint("请输入电话号码");
screen.addView(et_phoneNum);
Button bt_call = new Button(MainActivity_Java.this);
bt_call.setText("拨打电话");
screen.addView(bt_call);
bt_call.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String phone = et_phoneNum.getEditableText().toString();
Log.e("phone", phone);
Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + phone));
startActivity(intent);
}
});
return screen;
}
}