【Android开发API】用户界面-线形布局 - Linear Layout
Android组件---四大布局的属性详解

Android组件---四⼤布局的属性详解【声明】欢迎转载,但请保留⽂章原始出处→_→Android常见布局有下⾯⼏种:LinearLayout:线性布局。
所有的控件都是串在⼀条线上的。
RelativeLayout:相对布局。
所有的控件的位置,都是相对于⽗控件的。
FrameLayout:帧布局。
FrameLayout布局中的控件都是⼀层⼀层的。
帧布局每次添加的控件都显⽰在最上⾯,最后显⽰在界⾯上的是最后添加的⼀个控件。
TableLayout:表格布局。
表格布局可以实现的.⼀般可以使⽤线性布局实现。
AbsoluteLayout:绝对布局。
已经是废弃的状态,很少⽤了。
orientation:属性是指定线性布局的排列⽅向。
horizontal ⽔平。
线性布局默认的朝向是⽔平的。
vertical 垂直例如:android:orientation="vertical"gravity:指定当前控件⾥⾯的内容容显⽰位置。
(四⼤layout中均可使⽤)left 左边right 右边top 上边bottom 底边例如:android:gravity="center"gravity中的属性可以组合使⽤。
例如:android:gravity="bottom|right"layout_gravity:指定当前控件在⽗元素的位置。
(只在 LinearLayout 和 FrameLayout 中有效)left 左边right 右边top 上边bottom 底边centercenter_horizontalcenter_vertical例如:android:layout_gravity="center"另外,需要提⽰的是,对于 LinearLayout :当 android:orientation="vertical" 时,只有⽔平⽅向的设置才起作⽤,垂直⽅向的设置不起作⽤。
android开发4:Android布局管理器1(线性布局,相对布局RelativeLay。。。

android开发4:Android布局管理器1(线性布局,相对布局RelativeLay。
控件类概述View可视化控件的基类属性名称对应⽅法描述android:background setBackgroundResource(int)设置背景android:clickable setClickable(boolean)设置View是否响应单击事件android:visibility setVisibility(int)控制View的可见性android:focusable setFocusable(boolean)控制View是否可以获取焦点android:id setId(int)为View设置标识符,可通过findViewById⽅法获取android:longClickable setLongClickable(boolean)设置View是否响应长单击事件android:soundEffectsEnabled setSoundEffectsEnabled(boolean)设置当View触发单击等事件时是否播放⾳效android:saveEnabled setSaveEnabled(boolean)如果未作设置,当View被冻结时将不会保存其状态android:nextFocusDown setNextFocusDownId(int)定义当向下搜索时应该获取焦点的View,如果该View不存在或不可见,则会抛出RuntimeException异常android:nextFocusLeft setNextFocusLeftId(int)定义当向左搜索时应该获取焦点的Viewandroid:nextFocusRight setNextFocusRightId(int)定义当向右搜索时应该获取焦点的Viewandroid:nextFocusUp setNextFocusUpId(int)定义当向上搜索时应该获取焦点的ViewViewGroupView的⼦类,但是它可以充当其他控件的容器ViewGroup的⼦控件既可以是普通的View,也可以是ViewGroup。
【Android开发API】用户界面-布局 - Layouts

负责⼈:eoe_zvivi521原文链接:/guide/topics/ui/declaring-layout.html布局是用户界面在⼀个activity的架构。
它定义了布局架构,并且将所有元素呈现给用户。
可以用两种方式声明布局:在XML中声明UI元素。
Android提供了简单的XML元素和显示类以及⼦类对应,就像布局和widgets⼀样。
在运行时实例化布局元素* 。
应用能够通过编程创建显示对象和显示组对象(并且操作他们的属性)Android framework提供了非常灵活的方法来声明和管理应用UI。
例如,可以在XML中声明默认布局,屏幕元素会根据它们的属性显示。
接下来可以在应用中增加代码修改屏幕对象的状态,也可以在运行时修改在XML中声明的对象。
在XML中声明UI的好处是,可以更好地区分显示和控制这些行为的代码。
UI描述与应用代码无关,也就是说可以修改和调整UI布局但是不用修改源代码以及重新编译。
例如,能够为不同的屏幕目标、不用的设备屏幕⼤⼩、不同的语言创建不同的XML布局文件。
另外,在XML中声明布局使得UI更容易可视化,这样更容易调试问题。
就其本身而言,这个文档主要用于教会你如何在xml中声明布局。
如果您对运行时对象实例化感兴趣,那么请参考viewgroup以及view类说明。
⼀般来说,xml声明UI元素的词汇和类的命名以及方法名密切相关,元素根据类名、属性名根据方法名来命名。
实际上,能猜到什么XML属性对应⼀个类的方法,或者能够猜到哪个类对应给定的XML元素,这往往是直接的对应。
但是,注意并不是所有的词汇都是等同的。
在某些情况下,有的命名有些许不同。
例如,EditText元素有个text属性对应EditText.setText()方法。
提示:在Common Layout Objects学习更多不同的布局类型。
在hello views查看教程指南教程的集合。
写⼀个XML使用android的XML词汇,可以快速的设计UI布局和它们包含的屏幕元素。
线性布局(LinearLayout)_Android 开发权威指南_[共4页]
![线性布局(LinearLayout)_Android 开发权威指南_[共4页]](https://img.taocdn.com/s3/m/8ea6d1da6c85ec3a86c2c56a.png)
第4章我的UI我做主——用户界面开发基础4.5.2 线性布局(LinearLayout)工程目录:src\ch04\ch04_linearlayout线性布局是最常用的布局方式。
线性布局在XML布局文件中使用<LinearLayout>标签进行配置,如果使用Java代码,需要创建android.widget.LinearLayout对象。
线性布局可分为水平线性布局和垂直线性布局。
通过android:orientation属性可以设置线性布局的方向,该属性的可取值是horizontal和vertical,默认值是horizontal。
当线性布局的方向是水平时,所有在<LinearLayout>标签中定义的视图都沿着水平方向线性排列。
当线性布局的方向是垂直时,所有在<LinearLayout>标签中定义的视图都沿着垂直方向线性排列。
<LinearLayout>标签有一个非常重要的gravity属性,该属性用于控制布局中视图的位置。
该属性可取的主要值如表4.1所示。
如果设置多个属性值,需要使用“|”进行分隔。
在属性值和“|”之间不能有其他符号(例如,空格、制表符等)。
表4.1 gravity属性的取值属性值描述top 将视图放到屏幕顶端bottom 将视图放到屏幕底端left 将视图放到屏幕左侧right 将视图放到屏幕右侧center_vertical 将视图按垂直方向居中显示center_horizontal 将视图按水平方向居中显示center 将视图按垂直和水平方向居中显示在屏幕上添加3个按钮,并将它们右对齐,代码如下。
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent" android:gravity="right"><Button android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="按钮1" /><Button android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="按钮2" /><Button android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="按钮3" /></LinearLayout>使用上面的XML布局文件后,将得到如图4.19所示的效果。
LinearLayout线性布局搭配权重属性的使用

LinearLayout线性布局搭配权重属性的使⽤在开发中,我们是通过布局来完成应⽤界⾯的搭配的,通过各种布局,我们可以完成各种复杂的界⾯设计。
⽽LinearLayout也就是我们说的线性布局,这个⽐较简单⽽且使⽤很⼴泛的⼀种布局。
下⾯我们通过⼀个Demo来对这个布局进⾏学习。
我们先来看看效果图吧。
然后在来看看布局⽂件main_layout.xml<LinearLayout xmlns:android="/apk/res/android"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.example.linerlayoutdemo.MainActivity"><!-- 这个例⼦很简单主要知识点也就两个 --><!-- 1、布局嵌套,这个例⼦通过在⼀个线性布局⾥⾯嵌套了两个垂直⽅向线性布局 --><!-- 2、利⽤android:layout_weight这个属性平均分配宽度和⾼度,也就是权重属性 --><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="1"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:background="#120ff0"/><TextViewandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:background="#950ff0"/><TextViewandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:background="@android:color/darker_gray"/><TextViewandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:background="#000ff0"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="1"android:orientation="horizontal"><TextViewandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="#079ff0"/><TextViewandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="#000fca"/><TextViewandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="#000f65"/><TextViewandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@android:color/holo_blue_bright"/></LinearLayout></LinearLayout>这⾥我们⾸先说⼀下LinearLayout,线性布局有两个⽅向,⽔平和垂直⽅向。
AndroidLinearLayout线性布局详解

AndroidLinearLayout线性布局详解为了更好地管理Android应用的用户界面里的各组件,Android 提供了布局管理器。
通过使用布局管理器,Android应用图形用户界面具有良好的平台无关性。
推荐使用布局管理器来管理组件的分布、大小,而不是直接设置组件的位置和大小。
可以使用布局管理器嵌套布局管理器,即也可作为一个UI组件来使用。
LinearLayout可以控制组件横向排列或者纵向排列,内容不会换行,超出屏幕部分将不会显示出来。
学习图解LinearLayout 常用XML属性及方法【属性一】orientation 设置子组件的排列方式(单选)XML: android:orientation="horizontal"horizontal:横向排列vertical:纵向排列JAVA :linearLayout.setOrientation(LinearLayout.VERTICAL);LinearLayout.HORIZONTAL 横向排列LinearLayout.VERTICAL 纵向排列【属性二】gravity 设置子组件的对齐方式(多选)XML: android:gravity="center"JAVA :linearLayout.setGravity(Gravity.CENTER);【属性三】baselineAligned 设置子元素基准线对弃,默认为true 基准线:打开的英语练习本,那条红线就是基准线XML: android:baselineAligned="false"JAVA: linearLayout.setBaselineAligned(true);代码:true<LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:baselineAligned="true"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@android:color/holo_red_light" android:padding="20dp"android:text="text1"android:textSize="30sp"></TextView><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@android:color/holo_blue_light" android:padding="10dp"android:text="text2"android:textSize="16sp"></TextView></LinearLayout>效果:【搭配属性三】baselineAlignedChildIndex LinearLayout的基准线以他的第几个子元素为准,下标从0开始一个LinearLayout 里面有很多 textview ,每一个 textview 都有自己的基准线,那么LinearLayout可能也是另一个LinearLayout的子元素,作为子元素baselineAlignedChildIndex 就决定这他的一个基准线XML:android:baselineAlignedChildIndex="0"JAVA:linearLayout.setBaselineAlignedChildIndex(0);代码:⭐注意内部的LinearLayout,后面将在第二个LinearLayout上添加baselineAlignedChildIndex ,搭配 baselineAligned="false" 使用<LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:baselineAligned="false"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@android:color/holo_blue_light" android:text="这是text2"android:textSize="20sp"></TextView><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@android:color/holo_red_light" android:text="这是text1"android:textSize="30sp"></TextView><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@android:color/holo_green_dark" android:text="这是text2"android:textSize="15sp"></TextView></LinearLayout><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="这是text4"android:textSize="25sp"android:background="@android:color/holo_orange_light" ></TextView><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@android:color/black" android:text="text"android:textColor="@android:color/white"android:textSize="15sp"></TextView></LinearLayout>效果:⭐总结•默认LinearLayout是没有基准线的,从图一和图三的对比可知。
线性布局(LinearLayout)_Android应用开发全程实录_[共2页]
![线性布局(LinearLayout)_Android应用开发全程实录_[共2页]](https://img.taocdn.com/s3/m/e02813813c1ec5da50e270fc.png)
第3章 我的界面我作主——Activity 和布局管理器41 实例:举个例子,如果屏幕密度为160,这时dp 、sp 和px 是一样的。
1dp=1sp=1px ,但当使用px 单位的时候,如果屏幕大小不变(假设还是3.2英寸),而屏幕密度变成了320。
那么假如原来控件的宽度设成160px ,这时候就会发现,该控件在320密度的屏幕下短了一半。
但如果设置成160dp 或160sp 的话。
系统会自动将width 属性值设置成320px 。
还有一些不常用的单位,罗列如下。
in (英寸):基于屏幕物理尺寸的长度单位。
mm (毫米):基于屏幕物理尺寸的长度单位。
pt (磅):1/72英寸。
线性布局(LinearLayout )是一个一行或者一列只能放置一个控件的布局。
分为垂直线性布局和水平线性布局。
我们通过一个实例来学习,运行效果如图3-16所示。
实例:工程目录:EX_03_03<?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="/apk/res /android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> <TextView android:text="第一列" android:gravity="center_horizontal" android:background="#aa0000" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> <TextView android:text="第二列" android:gravity="center_horizontal" android:background="#00aa00" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> <TextView android:text="第三列" android:gravity="center_horizontal" android:background="#0000aa" android:layout_width="wrap_content"▲图3-16 线性布局。
Android常见界面布局线性布局LinearLayout优秀文档

第2章 Android常见界面布局 —第—2章移动An软dr件oid开常发见(界A面nd布ro局id) 第—2—章移动An软dr件oid开常发见(界A面nd布ro局id) 4第—2—线章移性动A布n软局dr件Loind开e常发a见rL(a界Ayo面nud布tro局id)
布局可以分为水平线性布局和 —第—2章移动An软dr件oid开常发见(界A面nd布ro局id)
线第4 性2线章布性局A布n(局drLLoiinndee常aar见rLLaa界yyoo面uut布t)局是以水平或者垂直的方式来显示界面中添加的控件,因此线性布局可以分为水平线性布局和垂直线性布局两 种第—。2—章移动An软dr件oid开常发见(界A面nd布ro局id) 4 线性布局LinearLayout 4第2线章性A布n局drLoinde常a见rLa界yo面u布t 局
第2章 Android常见界面布局
——移动软件开发(Android)
2.4 线性布局LinearLayout
线性布局(LinearLayout)是
4 线性布局LinearLayout
以水平或者垂直的方式来显示 —4 —线移性动布软局件Lin开e发arL(aAyonudtroid)
4第2线章性A布n局drLoinde常a见rLa界yo面u布t 局 第—2—章移动An软dr件oid开常发见(界A面nd布ro局id) 第4 2线章性A布n局drLoinde常a见rLa界yo面u布t 局
垂直线性布局两种。下面我们 第2章 Android常见界面布局
第线2性章布局An(drLoiinde常ar见La界yo面ut布)局是以水平或者垂直的方式来显示界面中添加的控件,因此线性布局可以分为水平线性布局和垂直线性布局两 种。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
翻译作者:⼩手冰凉
预计时间:2012-8-4原文链接:/guide/topics/ui/layout/linear.html
线性布局是⼀个视图组,它所有的⼦视图都在⼀个方向对齐,水平或者垂直。
你可以指定布局的方向通过android:orientation 属性。
线性布局的所有⼦视图排列都是⼀个靠着另⼀个,因此垂直列表每行仅仅有⼀个⼦视图,不管有多宽。
水平列表只能有⼀行的高度(最高⼦视图的高度加上边距距离)。
线性布局对于每⼀个⼦视图涉及到边缘在⼦视图和权重(左边或者右边以及中间对齐)之间。
布局权重
线性布局支持给个别的⼦视图设定权重,通过android:layout_weight 属性。
就⼀个视图在屏幕上占多⼤的空间而言,这个属性给其设定了⼀个重要的值。
⼀个⼤的权重值,允许它扩⼤到填充父视图中的任何剩余空间。
⼦视图可以指定⼀个权重值,然后视图组剩余的其他的空间将会分配给其声明权重的⼦视图。
默认的权重是0.
例如,如果有三个文本框,其中两个声明的权重为1,另外⼀个没有权重,没有权重第三个文本字段不会增加,只会占用其内容所需的面积。
其他两个同样的会扩⼤以填补剩余的空间,在三个文本域被测量后。
如果第三个字段,然后给定的权重为2(而不是0),那么它现在的声明比其他的更重要,所以它得到⼀半的总的剩余空间,而前两个平均分配剩余的。
相同权重⼦视图
若在线性布局中创建占有相同空间的⼦视图,设置每个⼦视图的android:layout_height 属性值为"0dp"(对于垂直线性布局来说),或者设置每个⼦视图的android:layout_width 属性值为"0dp"(对于水平线性布局来说)。
然后在设置每个⼦视图的android:layout_weight 属性值为"1".
例⼦
1
2
3
4
5
6<?xml version"utf-8"?> <LinearLayout xmlns:android="/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="16dp" android:paddingRight="16dp"
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
android:orientation="vertical">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/to"/> <EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/subject"/> <EditText
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
android:hint="@string/message"/> <Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="@string/send"/>
</LinearLayout>
例⼦运行的结果如下图所示:
如果想更详细的了解线性布局的每个⼦视图的可用属性,可以参考youtParams
©eoe移动开发者社区
本文链接: /page/Linear_Layout。