LinearLayout把视图组织成一行或一列。子视图能被安排成垂直的或水平的。线性布局是非常常用的一种布局方式。
请看一个布局例子:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- 大的框架,横着布局 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<!-- 线性布局-竖着布局 -->
<LinearLayout
android:layout_width="100dp"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2" />
</LinearLayout>
<!-- 线性布局-横着着布局 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button4" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
效果图:
线性布局框架的一个属性表:
属性 |
描述 |
layout_width |
指定View或ViewGroup的宽度 |
layout_height |
指定View或ViewGroup的高度 |
layout_marginTop |
指定View或ViewGroup上方的额外空间 |
layout_marginBottom |
指定View或ViewGroup下方的额外空间 |
layout_marginLeft |
指定View或ViewGroup左侧的额外空间 |
layout_marginRight |
指定View或ViewGroup右侧的额外空间 |
layout_gravity |
指定View或ViewGroup中的子视图的排列位置 |
layout_weight |
指定指派给View或ViewGroup的额外空间尺寸 |
layout_x |
指定View或ViewGroup的x坐标 |
layout_y |
指定View或ViewGroup的y坐标 |
可以看到layout_width和layout_hight中经常有fill_parent,wrap_content,match_parent来区分宽度和高度。这三者什么区别呢?
fill_parent:
设置一个构件的布局为fill_parent将强制性地使构件扩展,以填充布局单元内尽可能多的空间。这跟Windows控件的dockstyle属性大体一致。设置一个顶部布局或控件为fill_parent将强制性让它布满整个屏幕。
wrap_content:
设置一个视图的尺寸为wrap_content将强制性地使视图扩展以显示全部内容。以TextView和ImageView控件为例,设置为wrap_content将完整显示其内部的文本和图像。布局元素将根据内容更改大小。设置一个视图的尺寸为wrap_content大体等同于设置Windows控件的Autosize属性为True。
match_parent:
Android2.2中match_parent和fill_parent是一个意思 .两个参数意思一样,match_parent更贴切,于是从2.2开始两个词都可以用。那么如果考虑低版本的使用情况你就需要用fill_parent了
视图和视图组概念:
视图:就是显示在屏幕上的一个组件(Widget)。View的例子:按钮(Button)、标签(TextView)和文本框(EditText)。每个“视图”(View)都继承自基类android.view.View。
视图组:可以包含一个或多个View。ViewGroup本身就是一种特殊的View,它提供了一个布局,可以使用这个布局去组织一系列的View视图。LinearLayout和FrameLayout。每个“ViewGroup”都继承自基类android.view.ViewGroup
分享到:
相关推荐
Android开发学习23】界面布局之线性布局LinearLayout代码
Android常见的布局类型是线性布局(LinearLayout),包含布局方向设置、基本使用、常用使用案例以及实现均分案例(垂直均分、水平均分)、以及线性布局嵌套,实现垂直且水平居中,整体按照“理论+案例”,分析清晰,...
AndroidStdio LinearLayout 线性布局写一个登录界面_顾缘君兮的博客-CSDN博客.html
Android移动应用开发线性布局LinearLayout的常用属性.pdf 学习资料 复习资料 教学资源
Android移动应用开发线性布局LinearLayout的weight属性简介.pdf 学习资料 复习资料 教学资源
Android中常用的5大布局方式有以下几种:...线性布局是Android开发中最常见的一种布局方式,它是按照垂直或者水平方向来布局,通过“android:orientation”属性可以设置线性布局的方向。属性值有垂直(vertical)和水平
Android 程序技术 本节课程内容:布局的创建和类型 PART 1 2.1 .1 布局的创建 常见布局的创建 关于布局 ...线性布局(LinearLayout)主要以水平或垂直方式来显示界面中的控件。当控件水平排列时,显
•Android---UI篇---LinearLayout(线性布局) • •Android---UI篇---WebView(网络视图) • •Andorid---UI篇---两种进度条(ProgressBar) • •Android---UI篇---Dialog(对话框) • •Android---UI篇---...
但是从面向对象的观点来看,ViewGroup 继承自 View,所以 ViewGroup组件布局——线性布局LinearLayout 是线性布局控件,它包含的子
LinearLayout是线性布局控件:要么横向排布,要么竖向排布,下面通过本篇文章给大家介绍Android布局之LinearLayout线性布局,涉及到android linearlayout 布局相关知识,对本文感兴趣的朋友一起学习吧
LinearLayout是线性布局控件,它包含的子控件将以横向或竖向的方式排列(通过android:orientation属性来控制),按照相对位置来排列所有的widgets或者其他的containers,超过边界时,某些控件将缺失或消失 2. 线性...
主要介绍了Android编程之线性布局LinearLayout用法,结合实例形式简单分析了Android线性布局的使用技巧,需要的朋友可以参考下
android 中常用的几种布局方式:LinearLayout ( 线性布局 )、LinearLayout ( 线性布局 )、TableLayout ( 表格布局 ),及控件的各种属性
用Android Studio 2.3.3做的线性布局例子,主要用到的标签有:<LinearLayout></LinearLayout>;主要用到的设置有:android:orientation="horizontal"、android:layout_weight="1"。
LinearLayout:线性布局,从左到右 第一种方法实现界面布局:手动写布局 第二种方法实现界面布局:布局文件(layout中的activty_main.xml) 其实就是标签的嵌套,注意单标签、双标签 拨打电话的实现: 布局activity_...
LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” xmlns:tools=”http://schemas.android.com/tools” android:id=”@+id/LinearLayout” android:layout_width=”fill_parent” android...