Android学习手记(5) 基本UI布局
1、View和ViewGroup
Activity是Android应用程序的基本管理单元,Android的每一个窗口都是通过一个Activity来定义的,但是Activity并不能直接用来显示窗口。我们需要调用setContentView(View view)来显示这个窗口。也就是说,真正用来显示窗口的是View类及其子类。
View主要负责绘制元素和事件处理,提供了许多用于绘制界面的子类。比如Button类,TextView类,EditText类等等。我们可以通过这些子类控件来进行一些简单的界面绘制。
ViewGroup是View的子类,主要作用是容纳其他元素。ViewGroup是一个抽象类,所以容纳其他元素充当一个容器的任务实际上是通过其子类完成的。比如FrameLayout类、LinearLayout类、AbsoluteLayout类、RelativeLayout类、TableLayout类、GridLayout类等等。
// Android APIs
ViewGroup
extends View
implements ViewParent ViewManager
java.lang.Object
↳ android.view.View
↳ android.view.ViewGroup
Known Direct Subclasses:
AbsoluteLayout, AdapterView<T extends Adapter>, CoordinatorLayout, DrawerLayout, FragmentBreadCrumbs, FrameLayout, GridLayout, LinearLayout, LinearLayoutCompat, PagerTitleStrip, RecyclerView, RelativeLayout, ShadowOverlayContainer, SlidingDrawer, SlidingPaneLayout, SwipeRefreshLayout, Toolbar, TvView, ViewPager
Known Indirect Subclasses:
AbsListView, AbsSpinner, ActionMenuView, AdapterViewAnimator, AdapterViewFlipper, AppBarLayout, AppCompatSpinner, AppWidgetHostView, BaseCardView, BrowseFrameLayout, and 43 others.
2、普通布局
A. FrameLayout
FrameLayout,框架布局。是一种最简单的布局类型,将所有的组件固定在界面的左上角,叠加显示,后一个组件在前一个组件之上显示。也就是说,后一个组件可能会覆盖前一个组件。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:text="Hello World!"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:text="Hello Android!"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
B. LinearLayout
LinearLayout,线性布局。所有的组件按照一个方向放置,其方向通过android:orientation=”horizontal”来控制,这个是水平放置组件,我们可以把horizonal改写为vertical,从而实现组件的竖直放置。
<LinearLayout
android:orientation="horizontal">
//组件
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:orientation="horizontal"> <TextView android:text="Hello World!"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:text="Hello Android!"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
C. AbsoluteLayout
AbsoluteLayout,绝对布局。这是一种不推荐的方式,因为其组件位置由左上角为坐标原点定义,当屏幕尺寸变化时,不能良好适应屏幕的显示需求,可能因为坐标问题在屏幕外面显示,从而用户无法对其操作。
<AbsoluteLayout>
<Button android:text="Hello Android!"
<!--其他代码--->
android:layout_x="100px"
android:layout_y="100px"/>
</AbsoluteLayout>
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:text="Hello World!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="600px"
android:layout_y="50px"/>
<TextView android:text="Hello Android!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="100px"
android:layout_y="100px"/>
</AbsoluteLayout>
D. RelativeLayout
RelativeLayout,相对布局。放置在相对布局上的组件可以设置其相对于子元素或者父元素的位置。通过指定相对父元素的位置来实现定位。
<RelativeLayout
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:id="@+id/button"
android:layout_below="@+id/radioFalse" //指定在父元素下方
android:layout_alignParentRight="true" //右端与父窗体右端对齐
android:layout_alignParentEnd="true" /> //下端与父窗体末尾对齐
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Is that right?"
android:id="@+id/textView"
android:textSize="50px"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" /> <RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="True"
android:id="@+id/radioTrue"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checked="true" /> <RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="False"
android:id="@+id/radioFalse"
android:layout_below="@+id/radioTrue"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checked="false" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:id="@+id/button"
android:layout_below="@+id/radioFalse"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
E. GridLayout
GridLayout,网格布局。这种布局把子视图存放在一个矩形网格中。网格是由被无数虚细线分割成多个单元格的可视区域组成。贯穿整个API的网格线通过网格索引数来指定。
<GridLayout
//其他代码省略
android:orientation="horizontal"
android:columnCount="4" //Grid列数
android:rowCount="5"> //Grid行数 <Button
android:layout_columnSpan="2" //指定占用列数
android:layout_rowSpan="2" //指定占用行数
android:layout_gravity="fill"/> //相对父View的位置
/GridLayout>
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="4"
android:orientation="horizontal"
android:paddingEnd="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:rowCount="5">
<Button
android:id="@+id/one"
android:text="1" />
<Button
android:id="@+id/two"
android:text="2" />
<Button
android:id="@+id/three"
android:text="3" />
<Button
android:id="@+id/devide"
android:text="/" />
<Button
android:id="@+id/four"
android:text="4" />
<Button
android:id="@+id/five"
android:text="5" />
<Button
android:id="@+id/six"
android:text="6" />
<Button
android:id="@+id/multiply"
android:text="×" />
<Button
android:id="@+id/seven"
android:text="7" />
<Button
android:id="@+id/eight"
android:text="8" />
<Button
android:id="@+id/nine"
android:text="9" />
<Button
android:id="@+id/minus"
android:text="-" />
<Button
android:id="@+id/zero"
android:layout_columnSpan="2"
android:layout_gravity="fill"
android:text="0" />
<Button
android:id="@+id/point"
android:text="." />
<Button
android:id="@+id/plus"
android:layout_gravity="fill"
android:layout_rowSpan="2"
android:text="+" />
<Button
android:id="@+id/equal"
android:layout_columnSpan="3"
android:layout_gravity="fill"
android:text="=" />
</GridLayout>
3、总结
以上就是几种布局的基本方法,其中包括FrameLayout、LinearLayout、AbsoluteLayout、RelativeLayout、GridLayout。使用过程中应该灵活使用,加上布局嵌套之类的。
版权声明:本文为博主原创文章,未经博主允许不得转载。
Android学习手记(5) 基本UI布局的更多相关文章
- Android学习笔记六:六大布局
六大界面布局方式包括: 线性布局(LinearLayout).帧布局(FrameLayout).表格布局(TableLayout).相对布局(RelativeLayout).绝对布局(Absolute ...
- Android学习笔记_3_四种布局
Android布局是应用界面开发的重要一环,在Android中,共有四种布局方式, 分别是:FrameLayout( 帧布局 ).LinearLayout (线性布局).TableLayout(表格布 ...
- Android学习笔记(九)——布局和控件的自定义
//此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! View是 Android中一种最基本的 UI组件,它可以在屏幕上绘制一块矩形区域,并能响应这块区域的各种事件 ...
- Android学习手记(4) BroadcastReceiver监听电池信息
Android 中,Broadcast是一种在应用程序之间进行传输信息的机制.BroadcastReceiver对发送过来的Broadcast进行过滤和响应.根据这种机制,我们可以获取电池现有电量等信 ...
- Android学习手记(3) Activity间传递数据
1. 简单数据传递 建立两个Activity,名称分别为MainActivity和TheAty,在MainActivity中新建一个Button,id为btnStartAty.在TheAty中新建一个 ...
- Android学习手记(1) Activity跳转
新建Project,并将主页命名为MainActivity. 创建一个Activity 在App上“右键->New->Activity->Empty Activity”, 将新建的A ...
- Android学习:代码控制UI界面示例
package allegro.test2; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; im ...
- android学习:关于RelativeLayout叠放布局的问题
RelativeLayout布局关于元素叠加的问题 1.RelativeLayout布局中的元素如果要实现元素叠加必须设置 RelativeLayout.ALIGN_PARENT_TOP 这样元素 ...
- Android学习笔记(七) 布局基础
一.概念 控件布局方法,就是指控制控件在Activity当中的位置.大小.颜色以及其他控件样式属性的方法.有两种方法可以控制布局: 在布局文件(xxx.xml)中完成控件的布局. 在JAVA代码中完成 ...
随机推荐
- C语言笔记(结构体与offsetof、container_of之前的关系)
关于结构体学习,需要了解:结构体的定义和使用.内存对齐.结构体指针.得到结构体元素的偏移量(offsetof宏实现) 一.复习结构体的基本定义和使用 typedef struct mystruct { ...
- C++学习笔记3——类的封装(1)
封装: 1.把属性和方法进行封装. 2.对属性和方法进行访问控制. class和struct的区别: class和struct的唯一区别是默认的访问权限不一样.struct的默认访问权限是public ...
- jquery 获取选中的文字.当前光标所在的位置等jquery-fieldselection 插件
写词典在线编辑器用到的一个功能 能获取选中的文字.当前的光标的位置 等位置,而且支持多个文本框一起操作 非常方便 git地址:https://github.com/localhost/jquery-f ...
- Node.js 入门(2)
1.http 请求 //调用Node.js自带的http模块 var http = require("http"); //调用http模块提供的函数createServer htt ...
- 嵌入式 现已发展为 IT行业的主流——高薪,且人才缺乏
嵌入式 现已发展为 IT行业的主流——高薪,且人才缺乏 硅谷芯微技术中心,注重实践操作,以实际项目带学员,让学员真正学到东西,达到企业用人标准,有兴趣的,可以前来了解,给自己多一个选择的机会,可以多家 ...
- JavaScript and html的关系
HTML--------------------------->DOM, BOM, Event Request/Response------------->Ajax 日期处理 http:/ ...
- 其实,SSL也不是配通了就什么都不管的~~
其中太多的中间人攻击需要去加强加固~~ 测试过A级是必须的!! https://www.ssllabs.com/ssltest/ 这网址两年前,我写过的哈
- [Android] createTrack_l
在分析AudioTrack的时候,第一步会new AudioTrack,并调用他的set方法.在set方法的最后调用了createTrack_l创建音轨.我们现在来分析createTrack_l的流程 ...
- POJ2253 Frogger(最短路)
题目链接. 题意: 从0号点,到1号点,找一条能通过的路,使得这条路中的最大的边,比其它所有可能的路中的边都小. 分析: 这题就是按着dijkstra写,写着写着觉得像是prim了. 其中d[n]表示 ...
- @RestController
/* * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Vers ...