1.构建xml布局文件

使用android提供的xml布局,可以快速构建UI界面,每个xml文件必须包含一个根元素,该xml文件位于res/layout/目录中。例如:

<?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" > <TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a TextView" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button" />
</LinearLayout>

2.加载xml布局文件

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//加载xml布局文件应该位于首行
setContentView(R.layout.activity_main);
}

3.属性ID

(1).xml中添加ID属性

android:id="@+id/button"

(2).xml中引用ID

android:id="@id/button"

(3).得到控件对象

Button myButton = (Button) findViewById(R.id.button);

4.布局的宽与高属性

所有视图都应该指定layout_width 与 layout_height属性,对该属性的描述主要有2个值

wrap_content根据控件内容的大小来定义当前控件整体大小

match_parent填满父类允许填满的空间位置,在 API Level 8之前使用fill_parent

一般在指定宽高时,推荐使用dp,而不是pixels。因为在不同的尺寸屏幕手机上,dp有更好的展示效果.

在LinearLayout布局中,假设定义了android:orientation="horizontal",且使用了android:layout_weight来表示子类控件的权重,此时子类控件推荐使用layout_width="0dp",子类控件将根据权重大小等比宽度;如果子类控件使用了layout_width="match_parent",将根据权重等比缩小。同样竖直方位也是如此!

5.layout_gravity与gravity

layout_gravity即设置当前视图相对于父类的位置摆放,gravity即控制当前视图的子类相对于父类的位置摆放。但有时layout_gravity不会起作用,原因很简单,就是忽略了父类的方位布局!

比如父类布局设置了android:orientation="vertical",而其子类有个button按钮,设置了android:layout_gravity="center_vertical",不会有任何效果,因为button遵循竖直排布,但只有水平方向的移动才会有效果!

因此,父类布局为vertical时,在设置layout_gravity时,只能控制水平方向的移动;在父类布局为horizontal时,在设置layout_gravity时,只能控制竖直方向的移动!而layout_gravity="center"即为可能有效果的那个方位居中!

6.margin与padding

margin主要控制当前视图相对于父类视图相间的位置距离大小

padding,比如一个Button按钮,text="Bt";如果指定了android:paddingLeft="30dp",意为Bt距离当前控件的左边为30dp大小

7.常见布局

(1).LinearLayout

LinearLayout为一个视图组,所有子类按照一单方向排列,必须指定android:orientation属性,该值为horizontal或者vertical,当使用子类控件使用权重layout_weight时,宽或者高推荐使用0dp

(2).RelativeLayout

RelativeLayout一个布局相对于另一个视图的位置,在上下左右或者相对于父类等等

<?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"
android:paddingLeft="16dp"
android:paddingRight="16dp" > <EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/reminder" /> <Spinner
android:id="@+id/dates"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@id/name"
android:layout_toLeftOf="@+id/times" /> <Spinner
android:id="@id/times"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@id/name" /> <Button
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@id/times"
android:text="@string/done" /> </RelativeLayout>

Android_Layout_xml布局的更多相关文章

  1. 前端框架 EasyUI (2)页面布局 Layout

    在 Web 程序中,页面布局对应用程序的用户体验至关重要. 在一般的信息管理类的 Web 应用程序中,页面结构通常有一个主工作区,然后在工作区上下左右靠近边界的区域设置一些边栏,用于显示信息或放置一些 ...

  2. TODO:Laravel 使用blade标签布局页面

    TODO:Laravel 使用blade标签布局页面 本文主要介绍Laravel的标签使用,统一布局页面.主要用到到标签有@yield,@ stack,@extends,@section,@stop, ...

  3. CSS HTML元素布局及Display属性

    本篇文章主要介绍HTML的内联元素.块级元素的分类与布局,以及dispaly属性对布局的影响. 目录 1. HTML 元素分类:介绍内联元素.块级元素的分类. 2. HTML 元素布局:介绍内联元素. ...

  4. 谈谈一些有趣的CSS题目(六)-- 全兼容的多列均匀布局问题

    开本系列,谈谈一些有趣的 CSS 题目,题目类型天马行空,想到什么说什么,不仅为了拓宽一下解决问题的思路,更涉及一些容易忽视的 CSS 细节. 解题不考虑兼容性,题目天马行空,想到什么说什么,如果解题 ...

  5. Xamarin+Prism开发详解五:页面布局基础知识

    说实在的研究Xamarin到现在,自己就没设计出一款好的UI,基本都在研究后台逻辑之类的!作为Xamarin爱好者,一些简单的页面布局知识还是必备的. 布局常见标签: StackLayout Abso ...

  6. 界面设计技法之css布局

    css布局之于页面就如同ECMAScript之于JS一般,细想一番,html就如同语文,css就如同数学,js呢,就是物理,有些扯远,这里就先不展开了. 回到主题,从最开始的css到如今的sass(l ...

  7. Android如何制作漂亮的自适布局的键盘

    最近做了个自定义键盘,但面对不同分辨率的机型其中数字键盘不能根据界面大小自已铺满,但又不能每种机型都做一套吧,所以要做成自适应,那这里主讲思路. 这里最上面的titlebar高度固定,下面输入的金额高 ...

  8. 页面布局class常见命名规范

    头:header 内容:content/container 尾:footer 导航:nav 侧栏:sidebar 栏目:column 页面外围控制整体布局宽度:wrapper 左右中:left rig ...

  9. Flex 布局教程:语法篇

    作者: 阮一峰 网页布局(layout)是CSS的一个重点应用. 布局的传统解决方案,基于盒状模型,依赖 display属性 + position属性 + float属性.它对于那些特殊布局非常不方便 ...

随机推荐

  1. hdu 4355 Party All the Time(三分搜索)

    Problem Description In the Dark forest, there is a Fairy kingdom where all the spirits will go toget ...

  2. HTML标签的offset、client、 scroll和currentStyle属性

    本文来自:http://www.cnblogs.com/quanhai/archive/2010/04/19/1715231.html offsetHeight = borderTopWidth + ...

  3. 使用Unicorn-engine 续1

    续上次,在ubuntu server 14.04交叉编译好后,下一步就是在windows上使用了. 在windows上,我主要是用python进行分析程序,因此我最初安装的是官网上的 unicorn- ...

  4. python 笔记4-- 函数式编程

    高阶函数 把函数作为参数传入,这样的函数称为高阶函数,函数式编程就是指这种高度抽象的编程范式. 在python中 函数也是一种变量 def add(x, y, f): return f(x) + f( ...

  5. hibernate某些版本(4.3)下报错 NoSuchMethodError: javax.persistence.Table.indexes()

    其实本来没啥大问题,但到网上查的时候发现了一些误人子弟的说法,所以还是记下来吧. 现象: hibernate从低版本升级到某一个版本时(我们是升到4.3.10)时,在程序启动时会报错: java.la ...

  6. mysql1主多从配置

    mysql一主多从的配置: 其实1主多从的配置与一主一从配置非常相似,现在主要讲讲一主多从的大概配置方法. 一, 1,master端开启binlog日志,并且设置server id=1. 2.重启服务 ...

  7. Android默认启动程序问题

    参考地址:http://www.cnblogs.com/Lewis/p/3316946.html 怎么让我们自己开发的Android程序设为默认启动呢?其实很简单,只要在AndroidManifest ...

  8. C# ?? 操作符示例

    static int? GetNullableInt() { return null; } static string GetStringValue() { return null; } static ...

  9. iOS之短信认证

    短信验证 现在很多的短信验证平台,我们比较常用的有移动开发者服务平台 根据短信验证文档来集成 1. 找到iOS短信验证的集成开发文档 2. 下载SDK和Demo目录结构  3. 运行Demo 4. 写 ...

  10. iBatis核心框架浅析

    1.1 iBatis配置与运行 1.dal 层的dao接口实现类通常会继承SqlMapClientDaoSupport.spring容器在初始化一个dao bean实例时,通常会注入两块信息DataS ...