布局:

在 android 中我们常用的布局方式有这么几种:

1.LinearLayout ( 线性布局 ) :(里面只可以有一个控件,并且不能设计这个控件的位置,控件会放到左上角)

线性布局分为水平线性和垂直线性二者的属性分别为: android:orientation="horizontal" android:orientation="vertical" 。

2.RelativeLayout (相对布局):(里面可以放多个控件,但是一行只能放一个控件)

附加几类 RelativeLayout 的属性供大家参考:

第一类 : 属性值为 true 或 false

android:layout_centerHrizontal 水平居中

android:layout_centerVertical 垂直居中

android:layout_centerInparent 相对于父元素完全居中

android:layout_alignParentBottom 贴紧父元素的下边缘

android:layout_alignParentLeft 贴紧父元素的左边缘

android:layout_alignParentRight 贴紧父元素的右边缘

android:layout_alignParentTop 贴紧父元素的上边缘

android:layout_alignWithParentIfMissing 若找不到兄弟元素以父元素做参照物

第二类:属性值必须为 id 的引用名“ @id/id-name ”

android:layout_below 在某元素的下方

android:layout_above 在某元素的上方

android:layout_toLeftOf 在某元素的左边

android:layout_toRightOf 在某元素的右边

android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐

android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐

android:layout_alignBottom本元素的下边缘和某元素的的下边缘对齐

android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐

第三类:属性值为具体的像素值,如 30dip , 40px

android:layout_marginBottom 离某元素底边缘的距离

android:layout_marginLeft 离某元素左边缘的距离

android:layout_marginRight 离某元素右边缘的距离

android:layout_marginTop 离某元素上边缘的距离

3.TableLayout (表格布局):(这个要和TableRow配合使用,很像html里面的table)

这个表格布局不像HTML中的表格那样灵活,只能通过TableRow属性来控制它的行而列的话里面有几个控件就是几列(一般情况)。如:

<TableLayout>

<TableRow>

<EditText></EditText>

<EditText></EditText>

</TableRow>

<TableRow>

<EditText></EditText>

<EditText></EditText>

</TableRow>

</TableLayout>

表示两行两列的一个表格。

android:gravity="center"书面解释是权重比。其时就是让它居中显示。它还可以动态添加里面的每行每列。如下代码所示:

/*根据id查找表格对象*/

TableLayout tableLayout = (TableLayout) findViewById(R.id.table01);

/*创建列对象*/

TableRow tableRow =newTableRow(this);

/*文本框对象*/

TextView temp =newTextView(this);

temp.setText("text的值");

/*将此文本添加到列中*/

tableRow.addView(temp);

android:stretchColumns="1,2,3,4"它的意思就是自动拉伸1,2,3,4列。

4.AbsoluteLayout (绝对布局):(里面可以放多个控件,并且可以自己定义控件的x,y的位置)

5.FrameLayout (帧布局):(里面可以放多个控件,不过控件的位置都是相对位置)

在它里面的控件都是按后面的一个控件叠加在前一个控件上来显示的,所有元素都被放置在最左上角。如:

<FrameLayout android:layout_width="wrap_content"

android:layout_height="wrap_content" android:layout_weight="1">

<ImageView android:id="@+id/iv1" android:layout_width="wrap_content"

android:layout_height="wrap_content" android:visibility="invisible"

android:src="@drawable/lotusleaf"></ImageView>

<ImageView android:id="@+id/f1" android:layout_width="wrap_content"

android:layout_height="wrap_content" android:src="@drawable/frog_right"

android:visibility="invisible"></ImageView>

</FrameLayout>

表示的是id为f1的控件叠加在id为iv1的控件上面显示

(LinearLayout 和 RelativeLayout 应该又是其中用的较多的两种。AbsoluteLayout 比较少用,因为它是按屏幕的绝对位置来布局的如果屏幕大小发生改变的话控件的位置也发生了改变。这个就相当于HTML中的绝对布局一样,一般不推荐使用 )

注意事项:

1 、各布局不要乱用各自的属性。比如把属于 AbsoluteLayout 布局的android:layout_xandroid:layout_y用到 LinearLayout 布局或 RelativeLayout 布局,或者把 RelativeLayout 布局的 below , rightof 等属性应用到其他布局中。这样做虽然不会报错,但这是白浪费感情的工作,根本达不到我们需要的效果。

2 、关于android:layout_width="fill_parent"android:layout_height="wrap_content",这是对每个布局宽和高的设置。wrap_content可表示随着其中控件的不同而改变这个布局的宽度或高度,类似于自动设置宽和高,fill_parent使布局填充整个屏幕,另外还有一种match_parent,它本质上和fill_parent一样,并从 API Level8 开始替代fill_parent

更多相关文章

  1. Android(安卓)之 CheckBox 详解
  2. Android学习笔记之——UI设计
  3. Android(安卓)使用selector改变按钮状态实例详解
  4. Android(安卓)-- 设置textview文字居中或者控件居中
  5. Animations使用 (四)
  6. Android之fill_parent和wrap_content
  7. Android界面基本属性
  8. Android(安卓)学习笔记--android――listview总结
  9. pull解析

随机推荐

  1. Android(安卓)getevent及sendevent分析
  2. Android中SQLite数据库的使用
  3. Android ADK with a standard Arduino Un
  4. Android系统启动流程 -- bootloader
  5. 转:Android——定位和地图
  6. Android(安卓)数据库初窥
  7. android 蓝牙4.0广播功能应用
  8. Android Studio 实时显示布局文件Preview
  9. Android实现TCP与UDP传输
  10. Android 应用冻结流程分析