什么是布局?

就是把界面中的控件按照某种规律摆放到指定的位置

布局的二种实现

代码
xml配置文件:res/layout目录下
注:也可以同时使用xml和代码

目录

[TOC]来生成目录:

            • 什么是布局?
            • 布局的二种实现
    • 目录
    • LinearLayout 线性布局
          • android:gravity和android:layout_gravity的区别
    • FrameLayout 帧布局
    • RelativeLayout 相对布局
          • RelativeLayout支持的二个xml属性
          • 控制子组件布局的内部类RalativeLayout.LayoutParams 此内部类的属性分二类
            • 1、 boolean
            • 2、 id型
          • layout_toRightOf,layout_toLeftOf(是一种靠拢动作)
    • GridLayout 网格布局
          • 常用属性

padding 内补丁
margin 外补丁
background 颜色
match_parent/fill_parent 匹配父类的内容
wrap_content 适应自己的内容

LinearLayout 线性布局

orientation=”vertical(垂直) | horizontal(平行)”

android:gravity和android:layout_gravity的区别

android:gravity:控件内部的元素(对内有效)
android:layout_gravity:控件所在父元素的位置(对外生效)

例如:

<?xml version="1.0" encoding="utf-8"?>"http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity">    

FrameLayout 帧布局

注:帧布局有点类似于awt的CardLayout都是把组件一个一个叠加到一起,
但CardLayout能将下面的组件移上来,但FrameLayout没有提供此功能

例如:

<?xml version="1.0" encoding="utf-8"?>"http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity">   "200dp"        android:background="@color/aqua"        android:layout_gravity="center"        android:layout_height="200dp" />    "150dp"        android:background="@color/crimson"        android:layout_gravity="center"        android:layout_height="150dp" />    "120dp"        android:background="@color/lawngreen"        android:layout_gravity="center"        android:layout_height="120dp" />    "80dp"        android:background="@color/springgreen"        android:layout_gravity="center"        android:layout_height="80dp" />    "60dp"        android:background="@color/fuchsia"        android:layout_gravity="center"        android:layout_height="60dp" />

RelativeLayout 相对布局

注1:注意XML中组件的顺序,不然会报错
注2:android新版本中组件的定义顺序没有关系

RelativeLayout支持的二个xml属性

android:gravity :设置该布局容器内各子组件的对齐方式
android:ignoreGravity:设置哪个组件不受gravity属性的影响

控制子组件布局的内部类RalativeLayout.LayoutParams 此内部类的属性分二类
1、 boolean
    相对父元素    alignParent...    center...(只有在父元素中才存在水平或垂直居中等)
2、 id型
    @+id和@id的区别    @+id/x1(添加新ID)    @id/x1(引用此ID)    相对于指定元素(根据ID指定)
layout_toRightOf,layout_toLeftOf(是一种靠拢动作)

例如:

"http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity">    

GridLayout 网格布局

常用属性

布局相关属性:rowCount、columnCount
子控件相关属性:layout_gravity=”fill_horizontal|fill_vertical”

例如:

"http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:rowCount="5"    android:columnCount="4"    tools:context=".MainActivity">   

注:Space标签的作用:挡住控件,让其不超出网格的范围

更多相关文章

  1. 详解android:scaleType属性
  2. Android(安卓)中自定义控件和属性(attr.xml,declare-styleable,T
  3. view的clickable属性和点击background颜色改变
  4. addStatesFromChildren 和跑马灯
  5. Android(Java):EditText相关
  6. Android的layout_weight属性释疑
  7. Android(安卓)TextView属性详解
  8. (转载)Android之Intent的基本使用
  9. intent

随机推荐

  1. Eclipse环境下格式化Android的代码风格
  2. Android(安卓)UI学习 - Tab的学习和使用
  3. Gradle DSL method not found: 'android(
  4. Android多线程同步转异步方法总结
  5. Android应用使用自定义字体
  6. Android(安卓)Studio 2.0 Preview发布,附
  7. Android(安卓)MimeType和MimeTypeMap的介
  8. Android(安卓)中文api (81)——InputMeth
  9. Android短信的发送和广播接收者实现短信
  10. android开发每日汇总【2011-11-02】