您的位置:首页 > 移动开发 > Android开发

Android基础学习之常用布局学习

2016-06-05 21:00 369 查看
对于一个刚刚开始接触android的新手来说,学习的大纲很重要,没有一个合理的大纲,结果总是学到后面的知识,发现牵扯到了之前的一些基础内容,还需要返回去看这些知识点,这样给人一种很乱的感觉,所以建议新手都建立自己的大纲去学习知识,

当然,我现在就是一名新手,大牛看到这篇博客,可以忽视我,下面我就说一下android中常用的布局知识。

android中常用的布局有:

LinearLayout     线性布局
RelativeLayout  相对布局
FrameLayout     框架布局 | 帧布局
TableLayout
表格布局
AbsoluteLayout  绝对布局    

下面先从LinearLayout开始,就是线性布局, 这是一种Android中最常用的布局之一,它将自己包含的子元素按照一个方向排列。

方向的设置通过Android:orientation=”vertical”(竖直)或者Android:orientation=”horizontal”(水平)来实现,这个属性是重要属性,所以大家要注意这点。

代码如下:

<span style="white-space:pre"> </span><LinearLayout
android:id="@+id/fourcontent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<span style="white-space:pre">	</span></LinearLayout>

一般使用线性布局,需要设置id属性,即标识这个布局的id、layout_width为布局的宽度、layout_height是布局的高度,当然还有最重要的属性orentation属性,用于设置布局的垂直还是水平方向的线性布局。
LinearLayout有两个非常相似的属性:android:gravity与android:layout_gravity。

区别在于:android:gravity用于设置View组件的对齐方式,而android:layout_gravity用于设置Container组件的对齐方式。

接下来是RelativeLayout,即相对布局,顾名思义,相对布局就是以某一个元素为参照物,来定位的布局方式。主要属性有:相对于某一个元素android:layout_below(在指定元素下边)、
android:layout_toLeftOf(在指定元素左边)相对于父元素的地方android:layout_alignParentLeft(对于父容器是否左对齐)、android:layout_alignParentRight(对于父容器是否右对齐)

代码如下:<RelativeLayout
android:id="@+id/top_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>下面是FrameLayout布局,所有添加到这个布局中的视图都以层叠的方式显示。第一个添加的组件放到最底层,最后添加到框架中的视图显示在最上面。上一层的会覆盖下一层的组件。
代码如下:<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#aa0000"
android:text="One"
android:gravity="bottom|right"/>
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#00aa00"
android:text="Two"
android:gravity="bottom|right"/>
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#0000aa"
android:text="Three"
android:gravity="bottom|right"/>
</FrameLayout>
下面是TableLayout布局,TableLayout没有边框的,它是由多个TableRow对象组成,每个TableRow可以有0个或多个单元格,每个单元格就是一个View。这些TableRow,单元格不能设置layout_width,宽度默认是fill_parent的,只有高度layout_height可以自定义,默认是wrap_content。

代码如下:<TableLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TableRow>
<TextView
android:text="column1"
android:padding="3dip" />
<TextView
android:text="column2"
android:padding="3dip" />
<TextView android:text="column3"
android:padding="3dip" />
</TableRow>
</TableLayout>
最后是AbsoluteLayout
 绝对布局 ,这个布局现在基本用不到了,指的是指定组件的左上角绝对坐标来指定组件的布局,由于现在的手机屏幕尺寸以及分辨率的问题,这种布局就不能兼容不同尺寸屏幕和分辨率的手机,可以让子元素指定准确的x和y坐标值,并显示在屏幕上。其中坐标(0, 0)为左上角,当向下或向右移动时,坐标值将变大。

代码如下:

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/AbsoluteLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/txtIntro"
android:text="绝对布局"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_x="20dip"
android:layout_y="20dip">
</TextView>
</AbsoluteLayout>
当然还有其他布局方式,这里只是讲了一下android中常用的几个布局方式,以后会根据学习的内容定期写一些博客。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 布局