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代码中完成 ...
随机推荐
- delphi 创建数据库配置文件(TIniFile)
一.有必要了解INI文件的结构: ;注释 [小节名] 关键字=值 ... ---- INI文件允许有多个小节,每个小节又允许有多个关键字, “=”后面是该关键字的值. ---- 值的类型有三种:字符串 ...
- POJ1700:Crossing River(过河问题)
POJ1700 题目链接:http://poj.org/problem?id=1700 Time Limit:1000MS Memory Limit:10000KB 64bit IO ...
- Github 上利用github pages 部署站点
一:起始 准备项目,如果你在github上已有项目,则无需新建,如果你要新起一个项目,则需先在github上创建一个项目 本文以已创建好的 github/TestGitPage 为例. 二:设置gi ...
- The Managed Metadata Service or Connection is currently not available
Does the following error message looks familiar to you? when you go to site Actions -> Site Sett ...
- Java基础学习第一天
================每日必读==================== 写代码: 1.明确需求.我需要实现什么需求? 2.分析思路.我需要怎么实现需求? 3.确定步骤.我的每一部分思路需要使 ...
- JS之路——字符串函数
JS自带函数concat将两个或多个字符的文本组合起来,返回一个新的字符串.var a = "hello";var b = ",world";var c = a ...
- iOS项目管理:目录结构和开发流程
iOS项目管理:目录结构和开发流程 最近正在做一个大版本的更新,现在在重构中.... 发现很多人在一个项目的开始不知道开发流程是什么,也不是非常清晰的知道一个项目该有目录结构.如果项目小或者是 ...
- 让乔布斯立足肩上的C语言之父
2011年,人们对乔布斯的去世记忆深刻,但这一年还有另一位本应获得同样关注的人物也与世长辞,他就是C语言之父丹尼斯·里奇(Dennis Ritchie). 不过,并非所有人都没能正确认识到里奇所曾作出 ...
- 把 图片 资源文件 编译到dll
今天盘古 lucene的改了下.然后 里面有很多文件 . 还有一些 生成多音字的 汉语词典等. 索性一下子编译到dll里面 . 就不在项目里面设置 这些文件的目录了 然后找了下.愣是没找到. 后来发现 ...
- 【java】基于Tomcat的WebSocket转帖 + 自己理解
网址:http://redstarofsleep.iteye.com/blog/1488639 原帖时间是2012-5-8,自己书写时间是2013年6月21日10:39:06 Java代码 packa ...