什么是布局?

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

布局的二种实现

代码
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 中自定义控件和属性(attr.xml,declare-styleable,TypedA
  2. view的clickable属性和点击background颜色改变
  3. Android的layout_weight属性释疑
  4. Android Material Design 之CardView 属性解析及使用
  5. 图文详解Android属性动画
  6. Android ScrollView去掉滚动条及ScrollView属性

随机推荐

  1. android UI进阶之仿iphone的tab效果 (Andr
  2. Android(安卓)image的示例 拍照显示
  3. Unity3D调用android方法(非插件方式)
  4. Android札记【2】——系统的认识心中的An
  5. 自己动手写一个简单的Android下拉刷新
  6. Android(安卓)补间动画(动画列表,平移,旋转
  7. Android之崩溃日志管理(原创)
  8. android应用安全——签名机制
  9. android 面试(二)
  10. Android(安卓)操作系统的内存回收机制