我们的软件是由好多个界面组成的,而每个界面又由N多个控件组成,Android中借助布局来让各个空间有条不紊的摆放在界面上。

可以把布局看作是一个可以放置很多控件的容器,它可以按照一定的规律调整控件的位置,从而实现精美的界面。

布局中也可以放置布局,通过多层布局的嵌套,实现比较复杂的界面。

Android提供了四种基本布局:LinearLayout、RelativeLayout、FrameLayout、TableLayout

LinearLayout:

LinearLayout称为线性布局,正如其名字一样,这个布局中的所有控件在线性方向上依次排列。

LinearLayout通过orientation属性来指定排列方向是垂直还是水平,如果值为vertical,控件为垂直排列,如果值为horizantal,控件为水平排列

代码:

activity_main.xml:

  1. <LinearLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context=".MainActivity"
  7. android:orientation="vertical"
  8.  
  9. >
  10.  
  11. <Button
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="BUTTON1"
  15. android:id="@+id/button1"/>
  16.  
  17. <Button
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:text="BUTTON2"
  21. android:id="@+id/button2"/>
  22.  
  23. <LinearLayout
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:orientation="horizontal">
  27.  
  28. <Button
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:text="BUTTON3"/>
  32.  
  33. <Button
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:text="BUTTON4"/>
  37.  
  38. </LinearLayout>
  39.  
  40. </LinearLayout>

  以上代码在模拟器中显示为

  

  

从上面的代码我们可以看出,线性布局中,android:orientation属性控制了内部控件的布局方向,vertical为垂直布局,horizontal为水平布局,Android系统默认线性布局为水平方向(horizaontal)。

线性布局中还有一个属性android:gravity来控制布局内子元素的重力方向,它有以下多个参数可供选择:

center、  bottom、center_horizontal、center_vertital、clip_horizontal、clip_vertital、         end、fill、       fill_horizontal、fill_vertital、   left、right、start、top。

LinearLayout还有另外一个重要的属性——android:layout_weight,这个属性可以让我们使用比例的方式来指定控件的大小,在手机屏幕适配型方面可以起到非常重要的作用。

    代码示例:

    activity_main.xml:

  1. <LinearLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context=".MainActivity"
  7. android:orientation="horizontal"
  8. >
  9.  
  10. <EditText
  11. android:layout_width="0dp"
  12. android:layout_height="wrap_content"
  13. android:id="@+id/usernameInput"
  14. android:text="请输入你的用户名"
  15. android:layout_weight="7"/>
  16.  
  17. <Button
  18. android:layout_width="0dp"
  19. android:layout_height="wrap_content"
  20. android:id="@+id/loginButton"
  21. android:text="登录"
  22. android:layout_weight="3"/>
  23. </LinearLayout>

  运行结果为:

  

  EditText占屏幕的7/10  Button占屏幕的3/10.原理很简单,先将两个控件所占比例相加为屏幕总比例,然后用各自所占比例相除

  

Android笔记(七) Android中的布局——线性布局的更多相关文章

  1. Android 笔记之 Android 系统架构

    Android笔记之Android系统架构 h2{ color: #4abcde; } a{ color: blue; text-decoration: none; } a:hover{ color: ...

  2. iOS流布局UICollectionView系列七——三维中的球型布局

      摘要: 类似标签云的球状布局,也类似与魔方的3D布局 iOS流布局UICollectionView系列七——三维中的球型布局 一.引言 通过6篇的博客,从平面上最简单的规则摆放的布局,到不规则的瀑 ...

  3. Android Fragment之间的通信(用fragment替换掉XML布局文件中的一个线性布局)

    1.XML布局 (1)主界面 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xml ...

  4. Android布局— — —线性布局

    以水平或垂直的方式显示界面中的控件 线性布局 语法格式: <LinearLayout xmlns:android="http://schemas.android.com/apk/res ...

  5. Android笔记(七十六) 点菜DEMO

    一个朋友让看一下他的代码,一个点菜的功能,他和我一样,初学者,代码比我的都混乱,也是醉了,干脆想着自己写个demo给他看,原本想着听简单,半个小时应该就可以搞定,真正写的时候,画了3h+,汗颜... ...

  6. Android笔记(七十三) Android权限问题整理 非常全面

    Android权限系统非常庞大,我们在Android系统中做任何操作都需要首先获取Android系统权限,本文记录了所有的Android权限问题,整理一下分享给大家. 访问登记属性 android.p ...

  7. Android笔记(七十二) Style和Theme

    我们尝尝需要使用setText.setColor.setTextSize等属性来设置控件的样式,但是每个控件都需要设置这些属性,工作量无疑是巨大的,并且后期维护起来也不方便. Style Androi ...

  8. Android笔记(七十) AlertDialog

    alertdialog可以在当前界面中弹出一个对话框,这个对话框在界面所有元素之上,可以屏蔽掉其他控件的交互能力,因此alertdialog常用于一些重要的内容警告. 使用AlertDialog.Bu ...

  9. Android笔记: Android版本号

    由于有2套版本号 总是对应不准 记下来做过标记 Android 4.3 ----18 Android 4.2---17 Android 4.1---16 Android 4.0.3---15Andro ...

随机推荐

  1. linux中解决出现:^H^H^H^H

    解决出现:^H^H^H^H 把stty erase ^H 添加到.bash_profile中 vim /etc/profile stty erase ^H su root source /etc/pr ...

  2. 【Leetcode_easy】1021. Remove Outermost Parentheses

    problem 1021. Remove Outermost Parentheses 参考 1. Leetcode_easy_1021. Remove Outermost Parentheses; 完

  3. Juniper总结

    Juniper的路由器分为两个部分——RE和PFE.不过貌似大部分路由器都分为这两个部分.... Routing Engine: 当密码授权通过之后,用户就进入了RoutingEngine中,在其中可 ...

  4. Mac下WordPress4.1安装使用笔记

    WordPress简介         WordPress是一种使用PHP语言开发的博客平台,用户可以在支持PHP和MySQL数据库的服务器上架设属于自己的网站.也可以把 WordPress当作一个内 ...

  5. React-native/React 公告滚动组件(原生代码)

    编写不易, 希望大家点赞 import React, {PureComponent} from 'react'; import {Animated, Easing, View} from 'react ...

  6. rtsp向rtmp推流

    package com.awifi.video.media.test; import org.bytedeco.javacpp.avcodec; import org.bytedeco.javacv. ...

  7. [转帖]大数据hadoop与spark的区别

    大数据hadoop与spark的区别 https://www.cnblogs.com/adnb34g/p/9233906.html Posted on 2018-06-27 14:43 左手中倒影 阅 ...

  8. Django开发常用方法及面试题

    目录 1.对Django的认识? 2.Django .Flask.Tornado的对比 3.什么是wsgi,uwsgi,uWSGI? 4. django请求的生命周期? 5. 简述什么是FBV和CBV ...

  9. 第五章 模块之 logging、copy、re

    5.12 logging 日志模块 报警等级 CRITICAL = 50 # 最高FATAL = CRITICALERROR = 40WARNING = 30WARN = WARNINGINFO = ...

  10. PAT(B) 1078 字符串压缩与解压(Java)

    题目链接:1078 字符串压缩与解压 (20 point(s)) 题目描述 文本压缩有很多种方法,这里我们只考虑最简单的一种:把由相同字符组成的一个连续的片段用这个字符和片段中含有这个字符的个数来表示 ...