展会信息港展会大全

android LinearLayout详解 (图)
来源:互联网   发布日期:2015-10-13 12:45:00   浏览:2290次  

导读:LinearLayout是线性布局控件,它包含的子控件将以横向或竖向的方式排列,按照相对位置来排列所有的widgets或者其他的containers,超过边界时,某些控件将缺失或消失。因 此一个垂直列表的每一行只会有一个widget......

LinearLayout是线性布局控件,它包含的子控件将以横向或竖向的方式排列,按照相对位置来排列所有的widgets或者其他的containers,超过边界时,某些控件将缺失或消失。因 此一个垂直列表的每一行只会有一个widget或者是container,而不管他们有多宽,而一个水平列表将会只有一个行高(高度为最高子控件的高度加 上边框高度)。LinearLayout保持其所包含的widget或者是container之间的间隔以及互相对齐(相对一个控件的右对齐、中间对齐或 者左对齐)。

xml属性

android:baselineAligned;//是否允许用户调整它内容的基线。

android:baselineAlignedChildIndex;//当一个线性布局与另一个布局是按基线对齐的一部分,它可以指定其内容的基线对齐方式。

android:gravity;//指定如何在该对象中放置此对象的内容(x/y坐标值)。

android:orientation:设置它内容的对其方向(横向/竖向)。

LinearLayout还支持为其包含的widget或者是container指定填充权值。好处就是允许其包含的widget或者是 container可以填充屏幕上的剩余空间。这也避免了在一个大屏幕中,一串widgets或者是containers挤成一堆的情况,而是允许他们放 大填充空白。剩余的空间会按这些widgets或者是containers指定的权值比例分配屏幕。默认的 weight 值为0,表示按照widgets或者是containers实际大小来显示,若高于0的值,则将Container剩余可用空间分割,分割大小具体取决于 每一个widget或者是container的layout_weight及该权值在所有widgets或者是containers中的比例。例如,如果 有三个文本框,其中两个指定的权值为1,那么,这两个文本框将等比例地放大,并填满剩余的空间,而第三个文本框不会放大,按实际大小来显示。如果前两个文 本框的取值一个为2,一个为1,显示第三个文本框后剩余的空间的2/3给权值为2的,1/3大小给权值为1的。也就是权值越大,重要度越大。

如果LinearLayout包含子LinearLayout,子LinearLayout之间的权值越大的,重要度则越校如果有 LinearLayout A包含LinearLayout C,D,C的权值为2,D的权值为1,则屏幕的2/3空间分给权值为1的D,1/3分给权值为2的C。在LinearLayout嵌套的情况下,子 LinearLayout必须要设置权值,否则默认的情况是未设置权值的子LinearLayout占据整个屏幕。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:gravity="center_horizontal">

<!--

android:orientation="vertical" 表示竖直方式对齐

android:orientation="horizontal"表示水平方式对齐

android:layout_width="fill_parent"定义当前视图在屏幕上可以消费的宽度,fill_parent即填充整个屏幕。

android:layout_height="wrap_content":随着文字栏位的不同

而改变这个视图的宽度或者高度。有点自动设置框度或者高度的意思

layout_weight 用于给一个线性布局中的诸多视图的重要度赋值。

所有的视图都有一个layout_weight值,默认为零,意思是需要显示

多大的视图就占据多大的屏幕空 间。若赋一个高于零的值,则将父视

图中的可用空间分割,分割大小具体取决于每一个视图的

layout_weight值以及该值在当前屏幕布局的整体

layout_weight值和在其它视图屏幕布局的layout_weight值中所占的比率而定。

举个例子:比如说我们在 水平方向上有一个文本标签和两个文本编辑元素。

该文本标签并无指定layout_weight值,所以它将占据需要提供的最少空间。

如果两个文本编辑元素每一个的layout_weight值都设置为1,则两者平分

在父视图布局剩余的宽度(因为我们声明这两者的重要度相等)。如果两个

文本编辑元素其中第一个的layout_weight值设置为1,而第二个的设置为2,

则剩余空间的三分之二分给第一个,三分之一分给第二个(数值越小,重要度越高)。

-->

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="BB"

/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="BB"

/>

</LinearLayout>

需要注意的是LinearLayout布局不会换行:当组件排到容器尽头时,其余的组件将不会被显示。

先明确几个概念的区别:

padding margin都是边距的含义,关键问题得明白是什么相对什么的边距.

padding是控件的内容相对控件的边缘的边距.

margin是控件边缘相对父空间的边距.

android:gravity属性是对该view 内容的限定.比如一个button 上面的text. 你可以设置该text 在view的靠左,靠右等位置.该属性就干了这个.

android:layout_gravity是用来设置该view中的子view相对于父view的位置.比如一个button 在linearlayout里,你想把该button放在靠左,靠右等位置就可以在linearlayout中通过该属性设置.

二:属性效果:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout android:id="@+id/custom_titlebar"

xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="wrap_content">

<Button

android:id="@+id/imageViewLoginState"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1"

android:text="1">

</Button>

<Button

android:id="@+id/imageViewLoginState1"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="2"

android:text="2"

>

</Button>

</LinearLayout>

二:接下来看一下 android:layout_width属性:

这里测试的是垂直布局android:orientation="vertical",所以针对组件的 android:layout_height属性;如果测试的水平布局,则需针对组件的 android:layout_width属性。

1.android:layout_height="fill_parent"

设置 :TextView_one属性android:layout_weight="1"

TextView_Two属性android:layout_weight="3"

效果:

接下来我们只是设置一下1.android:layout_height="wrap_content",其他属性不变

运行效果:

赞助本站

人工智能实验室

相关热词: LinearLayout

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港