android学习笔记(二)布局
来源:互联网 发布:plc梯形图编程规则 编辑:程序博客网 时间:2024/05/18 20:11
布局就是指android中如何摆放组件,android主要使用的布局有线性布局,绝对布局,相对布局,表格布局,框架布局这几种形式,下面首先来介绍一下,线性布局
一、线性布局
对于线性布局来说,就是指这个组件会在实际摆放时单独占用一行
具体摆放形式如下,关键字是linearlayout,以这个关键字开头就是线性布局,此布局下各组件会单独占用一行的空间,共有两种形式,水平和垂直,定义水平还是垂直需要用到android:orientation=”horizontal”关键字,horizontal是垂直,vertical即为水平
水平代码如下
<LinearLayout xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"<!-- 水平--> tools:context="com.example.helloandroid.SecondActivity" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:id="@id/textViewSet1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:text="I am Second" /> <CheckBox android:id="@+id/checkBox1" android:gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@id/textViewSet1" android:layout_alignParentTop="true" android:text="CheckBox" /> <Button android:id="@+id/buttonclose1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:text="Button" /></LinearLayout>
垂直代码如下
<LinearLayout xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" tools:context="com.example.helloandroid.SecondActivity" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:id="@id/textViewSet1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="I am Second" /> <CheckBox android:id="@+id/checkBox1" android:gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@id/textViewSet1" android:layout_alignParentTop="true" android:text="CheckBox" /> <Button android:id="@+id/buttonclose1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="Button" /></LinearLayout>
二、绝对布局
和名称一样,绝对布局就是通过设置每个组件的x,y来进行摆放的布局,关键字为AbsouluteLayout,需要设置的两个属性为android:layout_x和android:layout_y
示例代码:
<AbsoluteLayout xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" tools:context="com.example.helloandroid.SecondActivity" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:id="@id/textViewSet1" android:layout_x="32pt" android:layout_y="32pt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="I am Second" /> <Button android:id="@+id/buttonclose1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="76dp" android:layout_y="4dp" android:gravity="center_horizontal" android:text="Button" /> <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="63dp" android:layout_y="114dp" android:text="CheckBox" /></AbsoluteLayout>
三.相对布局
相对布局就是指其布局依赖于各组件,位置排放与参照的组件位置有关,相对布局的关键字为RelativeLayout,使用这个关键字后就代表此Activity的布局是相对布局,相对布局就需要设置如下属性
android:layout_below=”组件id”在该组件下方
android:layout_above=”组件id”在该组件上方
android:layout_toRightOf=”组件id”在该组件右方
android:layout_toLeftOf=”组件id”在该组件左方
参照代码:
<RelativeLayout xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" tools:context="com.example.helloandroid.SecondActivity" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:id="@id/textViewSet1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="I am Second" /> <CheckBox android:id="@+id/checkBox1" android:layout_below="@+id/buttonclose1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="CheckBox" /> <Button android:id="@+id/buttonclose1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@id/textViewSet1" android:text="Button" /></RelativeLayout>
四,表格布局
表格布局就相当于html中的table标签,在这里使用的关键字是TableLayout,此时组件相当于在一个表格中进行摆放,设置TableLayout后就需要设置以下属性
TableRow:行
表格布局的代码:
<TableLayout xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" tools:context="com.example.helloandroid.SecondActivity" xmlns:android="http://schemas.android.com/apk/res/android"> <TableRow> <TextView android:id="@id/textViewSet1" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="I am Second" /> </TableRow> <TableRow> <CheckBox android:id="@+id/checkBox1" android:layout_weight="2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="CheckBox" /> </TableRow> <TableRow> <Button android:id="@+id/buttonclose1" android:layout_weight="3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:text="Button" /> </TableRow></TableLayout>
五.框架布局
此布局主要用来存放一个组件,如果有多个组件会按照顺序显示最后一个,其他会被覆盖,但实际依然存在,示例代码如下,关键字为FrameLayout
<FrameLayout xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" tools:context="com.example.helloandroid.SecondActivity" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:id="@id/textViewSet1" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="I am Second" /> <CheckBox android:id="@+id/checkBox1" android:layout_weight="2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="CheckBox" /> <Button android:id="@+id/buttonclose1" android:layout_weight="3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:text="Button" /></FrameLayout>
这就是android主要使用的5种布局,另外这5种布局可以相互嵌套使用,和html的标签是类似的
- android学习笔记(二)布局
- android学习笔记(二)——layout布局
- Qt学习笔记(二)布局管理
- Android学习笔记(二):Android中的五大布局
- Android学习笔记之RecyclerView(二)-异布局Item实现
- Android学习笔记二十五之ListView多布局实现
- Android学习笔记二十五之ListView多布局实现
- Android学习杂记(二)布局
- Android学习0818<二>(界面布局)
- Android布局学习笔记
- Android 布局学习笔记
- Android学习笔记--布局
- android学习笔记-布局
- Android 学习笔记-布局
- Android学习笔记(二)(相对布局属性总结和跑马灯分析)
- android学习笔记(二)—— 常用布局与控件
- Android(OPhone) 学习笔记 - 布局
- Android 学习笔记 (3)界面布局
- nginx配置tomcat与配置rewrite。
- 机器学习中的数学(5)-强大的矩阵奇异值分解(SVD)及其应用
- Android取消标题栏&强制设置横(竖)屏方法
- 动态数组的模板类/求指定个数的素数
- 黑马程序员----面象对象及封装
- android学习笔记(二)布局
- AlphaAnimation无效
- ethercat Mark一下,后续在写
- 回顾系列2-部落战争
- tfs 安装相关问题
- Java中的instanceof关键字
- iOS唯一标示符引导
- Xcode 配置SVN详细步骤
- 使用ab进行压力测试详解