`
yidongkaifa
  • 浏览: 4050439 次
文章分类
社区版块
存档分类
最新评论

android开发基础知识(3) ---五大布局

 
阅读更多

android基础知识总结(3)

-------五大布局

Android程序可分为五中布局,其中线性布局(LinearLayout)和相对布局(RelativeLayout)比较常用,其他在一些特定条件下应用。

(一)线性布局(LinearLayout):

可分为横向和纵向两种,顾名思义,线性布局是把控件一个一个往界面上线性摆列。

设置线性布局为水平方向
android:orientation="horizontal"
设置线性布局为垂直方向
android:orientation="vertical"
设置正比例分配控件范围

android:layout_weight="1"
设置控件显示位置,这里为水平居中
android:gravity="center_horizontal"

每个控件在自己范围内可以设置自定义大小,代码如下:

<?xml version="1.0"encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<LinearLayout

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="horizontal"

android:gravity="center_horizontal"

android:layout_weight="2"

>

<ImageView

android:layout_width="156dp"

android:layout_height="76dp"

android:src="@drawable/my" />

<TextView

android:layout_width="62dp"

android:layout_height="wrap_content"

android:background="#FF0000"

android:text="@string/hello"

android:textColor="#000000"/>

<EditText

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="水平方向"

/>

</LinearLayout>

<LinearLayout

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1"

android:orientation="vertical" >

<ImageView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/my" />

<EditText

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/hi" />

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Canking"/>

</LinearLayout>

</LinearLayout>

(二)相对布局

相对布局就是按照控件的相对位置的方向来摆放控件。android手机屏幕的分辨率大不相同,所以为了考虑屏幕自适应的情况,可以使用相对布局,它的坐标取值范围都是相对的所以使用它来做自适应屏幕是正确的。

设置距父元素右对齐
android:layout_alignParentRight="true"
设置该控件在id为re_edit_0控件的下方
android:layout_below="@id/re_edit_0"
设置该控件在id为re_image_0控件的左边
android:layout_toLeftOf="@id/re_iamge_0"
设置当前控件与id为name控件的上方对齐
android:layout_alignTop="@id/name"
设置偏移的像素值
android:layout_marginRight="30dip"

代码为:

<?xml version="1.0"encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<EditText

android:id="@+id/re_edit_0"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="相对布局"

android:layout_alignParentRight="true"

/>

<ImageView

android:id="@+id/re_iamge_0"

android:layout_width="100dp"

android:layout_height="100dp"

android:layout_below="@id/re_edit_0"

android:src="@drawable/my" />

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="学习安卓ing"

android:textColor="@drawable/none"

android:layout_toRightOf="@id/re_iamge_0"

/>

<EditText

android:id="@+id/re_edit_1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/study"

android:layout_alignParentBottom="true"

/>

<ImageView

android:id="@+id/re_iamge_1"

android:layout_width="100dp"

android:layout_height="100dp"

android:layout_above="@id/re_edit_1"

android:layout_alignParentRight="true"

android:src="@drawable/my" />

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="#FF0000"

android:text="努力工作"

android:textColor="#000000"

android:textSize="18dip"

android:layout_toLeftOf="@id/re_iamge_1"

android:layout_above="@id/re_edit_1"

/>

</RelativeLayout>

相对布局经常和线性布局嵌套使用,灵活配合布局的技巧。使得用户体验简单自然。

三 帧布局
原理是在控件中绘制任何一个控件都可以被后绘制的控件覆盖,最后绘制的控件会盖住之前的控件。可以实现图片的重叠效果。


代码为:

<?xml version="1.0"encoding="utf-8"?>

<FrameLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<ImageView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/my"

/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/love"

android:background="#0011111f"

android:textSize="18dip"

/>

<ImageView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/my"

android:layout_gravity="bottom"

/>

<EditText

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="#00000000"

android:text="程序猿要珍惜身边的人"

android:layout_gravity="bottom"

/>

</FrameLayout>

此处文字背景为透明色,可以设置透明色来达到不同的界面体验感觉。

四绝对布局

绝对布局可以把控件摆放在屏幕范围内任何一个像素点上,可以设置任意控件的 在屏幕中 X Y 坐标点,和帧布局一样后绘制的控件会覆盖住之前绘制的控件。但手机显示屏分辨率的不同使得绝对布局会在不同机上显示效果有差距,所以少用此布局为好。

代码为:

<?xml version="1.0"encoding="utf-8"?>

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<ImageView

android:layout_width="120"

android:layout_height="120"

android:src="@drawable/my"

android:layout_x="80dip"

android:layout_y="50dip"

/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="当前坐标点 x = 80dip y = 50dip"

android:background="#1f000aaa"

android:textColor="#FF0000"

android:textSize="18dip"

android:layout_x="80dip"

android:layout_y="50dip"

/>

<ImageView

android:layout_width="120dp"

android:layout_height="120dp"

android:src="@drawable/my"

android:layout_x="50dip"

android:layout_y="300dip"

/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="当前坐标点 x = 50dip y =300 dip"

android:background="#FFFFFF"

android:textColor="#FF0000"

android:textSize="18dip"

android:layout_x="30dip"

android:layout_y="280dip"

/>

</AbsoluteLayout>

五表格布局

在表格布局中可以设置TableRow 可以设置 表格中每一行显示的内容 以及位置.


代码实现:

<?xml version="1.0"encoding="utf-8"?>

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<ImageView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/my"

android:layout_gravity="center"

/>

<TableRow

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:padding="10dip">

<TextView

android:text="姓名"

android:gravity="left"

/>

<TextView

android:text="电话"

android:gravity="right"/>

</TableRow>

<View

android:layout_height="2dip"

android:background="#FFFFFF"/>

<TableRow

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:padding="10dip">

<TextView

android:text="张三"

android:gravity="left"

/>

<TextView

android:text="123456789"

android:gravity="right"/>

</TableRow>

<TableRow

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:padding="10dip">

<TextView

android:text="李四"

android:gravity="left"

/>

<TextView

android:text="15810463139"

android:gravity="right"/>

</TableRow>

<TableRow

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:padding="10dip">

<TextView

android:text="王五"

android:gravity="left"

/>

<TextView

android:text="15810463139"

android:gravity="right"/>

</TableRow>

<TableRow

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:padding="10dip"

>

<TextView

android:text="姓名"

android:gravity="left"

/>

<TextView

android:text="性别"

android:gravity="right"/>

</TableRow>

<View

android:layout_height="2dip"

android:background="#FFFFFF"/>

<TableRow

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:padding="10dip"

>

<TextView

android:text="张三"

android:gravity="left"

/>

<TextView

android:text=""

android:gravity="right"/>

</TableRow>

<TableRow

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:padding="10dip">

<TextView

android:text="李四"

android:gravity="left"

/>

<TextView

android:text=""

android:gravity="right"/>

</TableRow>

<TableRow

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:padding="10dip">

<TextView

android:text="王五"

android:gravity="left"

/>

<TextView

android:text=""

android:gravity="right"/>

</TableRow>

</TableLayout>

设计良好的用户界面体验,是对这五种布局巧妙的嵌套配合使用,此处只是列出了布局的基本功能。


分享到:
评论

相关推荐

    Android 发展史、布局、常用控件等基础知识学习.pptx

    主要讲解Android发展史、Android五大布局、Android基础常用控件、Android开发工具等讲解

    Android课程设计---基于Android系统的音乐播放器设计与实现.doc

    Android平台五大优势特色: 1、开放性 2、挣脱运营商的束缚 3、丰富的硬件选择 4、不受任何限制的开发商 5、无缝结合的Google应用 图 Android 软件体系结构图 Android作为一个移动设备的平台,其软件层次结构包括...

    Android应用开发揭秘pdf高清版

    第1章 Android开发简介 1.1 Android基本概念 1.1.1 Android简介 1.1.2 Android的系统构架 1.1.3 Android应用程序框架 1.2 OMS介绍 1.2.1 OPhone介绍 1.2.2 Widget介绍 1.3 小结 第2章 Android开发环境搭建 2.1 ...

    android基础布局

    android开发,android简单的五大布局简单应用

    《Google Android开发入门与实战》.pdf

     本书内容上涵盖了用android开发的大部分场景,从android基础介绍、环境搭建、sdk介绍、market使用,到应用剖析、组件介绍、实例演示等方面。从技术实现上,讲解了5个android平台下的完整综合实例及源代码分析,...

    Android移动应用开发(第3版)卷Ⅰ基础篇 (Shane Conder, Lauren Darcey) PDF扫描版

     《Android移动应用开发(第3版)卷Ⅰ:基础篇》涵盖了Android开发从概念、技术到市场推广应用的全部主题,内容包括Android平台概览、Android应用程序设计精髓、Android用户界面设计精髓、Android应用程序设计精髓、...

    Google.Android开发入门与实战

     《Android开发入门与实战》内容上涵盖了用Android开发的大部分场景,从Android基础介绍、环境搭建、SDK介绍、Market使用,到应用剖析、组件介绍、实例演示等方面。从技术实现上,讲解了5个Android平台下的完整综合...

    ARM Cortex-A8和Android 4.x联动报警系统

    本教程共分五大部分内容,1 Android应用开发 2 Android系统移植 3 Cortex a8裸机接口开发 4 Android设备驱动开发 5 综合项目实战。第一部分课程从最基础的Android应用开发环境搭建开始,简单讲解了Android界面及事件...

    android知识大总结【邯院】

    JAVA基础知识点总结.doc 第1章 Java程序设计概述 第2章 Java语言的开发环境 第3章 Java程序的基本结构 第4章 对象和类 第5章 继承和多态 第6章 泛型程序设计 第7章 接口和内部类 第8章 异常处理 第9章 反射,代理,...

    工程硕士学位论文 基于Android+HTML5的移动Web项目高效开发探究

    鉴于市场上用户的手机型号、种类、屏幕分辨率等参差不齐,传统方式根据主流系统分别开发相应的系统耗时又耗力,为了高效开发并节约开发项目成本,本文采用Android+HTML5相结合的方式进行移动端Web系统的设计研发工作...

    Android期末设计报告.docx

    移动互联网时代的到来极大的改变了我们的生活,而Android是一种以Linux为基础的开放源码操作系统迅速占领了智能机操作系统,所以在Android环境下开发显得尤为必要,本文详细介绍了在Android平台上以Java为语言编写...

    基于安卓Android学生考勤信息管理系统客户端+数据库源码+开题报告及任务书.zip

    系统拟分为五大功能模块,分别是学生基础数据管理模块、考勤数据上传模块、考勤数据统计模块、考勤信息查询模块和用户管理模块。系统的使用者有系统管理员、领导、辅导员、任课教师和学生。 二、研究方法: 本学生...

    DDR2Layout指导手册

    layout_*的一些注意事项在Android UI的开发中layout_*应该是用的很多的,但其中有很多的坑想必大家也碰到过,今天就来写写自己碰到的!之后如果还有碰到就在添加。layout_widt... DDR Layout Guide-DDR布线规则与...

    软件专业调研报告(2).doc

    5、熟悉Android UI界面常用组件、常用布局方法、事件处理机制; 6、熟悉Android多线程设计、网络编程、数据存储与IO、多媒体开发; 7、熟悉Android端WebSocket开发,并且熟练使用OkHttp框架; 8、熟练掌握Android,...

    软件专业调研报告.doc

    5、熟悉Android UI界面常用组件、常用布局方法、事件处理机制; 6、熟悉Android多线程设计、网络编程、数据存储与IO、多媒体开发; 7、熟悉Android端WebSocket开发,并且熟练使用OkHttp框架; 8、熟练掌握Android,...

    软件专业调研报告(1).doc

    5、熟悉Android UI界面常用组件、常用布局方法、事件处理机制; 6、熟悉Android多线程设计、网络编程、数据存储与IO、多媒体开发; 7、熟悉Android端WebSocket开发,并且熟练使用OkHttp框架; 8、熟练掌握Android,...

    Android项目源码开源的 Material Design 豆瓣客户端.zip

    注意:本项目使用android studio开发,eclipse可能无法使用。 豆芽的名字取自“Douban, Yet Another”的缩写和中文词语“豆芽”的拼音正好相同的巧合。 取名之后,我才得知豆瓣的 Windows Phone 客户端的名字也...

    教你五分钟实现Android超漂亮的刻度轮播控件实例教程

    相信每个Android程序员都知道,我们每天的开发工作当中都在不停地跟View打交道,Android中的任何一个布局、任何一个控件其实都是直接或间接继承自View的,如TextView、Button、ImageView、ListView等。 一些接触...

    软件专业调研报告.docx

    5、熟悉Android UI界面常用组件、常用布局方法、事件处理机制; 6、熟悉Android多线程设计、网络编程、数据存储与IO、多媒体开发; 7、熟悉Android端WebSocket开发,并且熟练使用OkHttp框架; 软件专业调研报告全文...

Global site tag (gtag.js) - Google Analytics