本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。
原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/
LinearLayout把视图组织成一行或一列。子视图能被安排成垂直的或水平的。想知道LinearLayout是如何工作的,首先考虑一下典型的mail.xml文件。
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical">
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"/>
- </LinearLayout>
在main.xnl中,可以看到,根节点元素是<LinearLayout>,它包含了一个<TextView>元素。这个<LinearLayout>元素控制了它所包含的视图views的顺序。
每一个View和ViewGroup有一些共同的属性,一些例子:
属性 |
描述 |
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坐标 |
举个例子,<TextView>元素使用了fill_parent常量,所以的宽度填充了整个它所在布局的宽度。它的高度被设置成wrap_content常量,这意味着,它的高度将和它所在的布局一样。如果不想<TextView>占据整个屏幕,可以把layoutt_width元素设置成wrap_content,就像这样:
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/hello"/><spanstyle="font-family:Arial,Helvetica,sans-serif;">
- </span>
以上的代码,把TextView的宽度设置成和它所包含的文本的长度一样。
请看如下的布局:
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical">
- <TextView
- android:layout_width="100dp"
- android:layout_height="wrap_content"
- android:text="@string/hello"/>
- <Button
- android:layout_width="160dp"
- android:layout_height="wrap_content"
- android:text="Button"/>
- </LinearLayout>
这里,就把TextView和Button的宽度设置成了一个固定的值。
分享到:
相关推荐
AndroidStdio LinearLayout 线性布局写一个登录界面_顾缘君兮的博客-CSDN博客.html
主要为大家详细介绍了AndroidUI组件LinearLayout线性布局,具有一定的实用性,感兴趣的小伙伴们可以参考一下
LinearLayout是线性布局控件:要么横向排布,要么竖向排布,下面通过本篇文章给大家介绍Android布局之LinearLayout线性布局,涉及到android linearlayout 布局相关知识,对本文感兴趣的朋友一起学习吧
Android移动应用开发线性布局LinearLayout的常用属性.pdf 学习资料 复习资料 教学资源
Android移动应用开发线性布局LinearLayout的weight属性简介.pdf 学习资料 复习资料 教学资源
Android开发学习23】界面布局之线性布局LinearLayout代码
用Android Studio 2.3.3做的线性布局例子,主要用到的标签有:<LinearLayout></LinearLayout>;主要用到的设置有:android:orientation="horizontal"、android:layout_weight="1"。
很多时候,线性布局需要嵌套其他布局,比如线性布局,相对布局等
Android应用开发
Android常见的布局类型是线性布局(LinearLayout),包含布局方向设置、基本使用、常用使用案例以及实现均分案例(垂直均分、水平均分)、以及线性布局嵌套,实现垂直且水平居中,整体按照“理论+案例”,分析清晰,...
android 线性布局LinearLayout实例代码,需要的朋友可以参考一下
关于linearlayout布局案例
主要介绍了Android编程之线性布局LinearLayout用法,结合实例形式简单分析了Android线性布局的使用技巧,需要的朋友可以参考下
线性布局(LinearLayout)是 Android 中最基本的布局之一,用于在界面上按照水平或垂直方向依次排列子视图。它是一种简单而灵活的布局方式,适用于许多应用场景,如列表项、工具栏、表单等等。 在 Android 开发中,...
Android源码LinearLayout实例,经典代码,是学习Android的好东西
android demo,LinearLayout的事件的的处理,LinearLayout的显示的隐藏。
主要介绍了Android应用借助LinearLayout实现垂直水平居中布局的方法,文中列举了LinearLayout线性布局下居中相关的几个重要参数,需要的朋友可以参考下
摘要:Java源码,Android源码,线性布局 一个基础级的Android线性布局实例——LinearLayout源码下载,帮助Android初学者了解线性布局的思路和实现方法,这里暂时没有安卓运行环境,所以没有抓到运行效果,还望理解,...