android的LinearLayout布局

简介:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?xml version= "1.0"  encoding= "utf-8" ?>
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
     android:layout_width= "fill_parent"
     android:layout_height= "fill_parent"
     android:paddingLeft= "16dp"
     android:paddingRight= "16dp"
     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>
<!--    android:orientation= "vertical"  代表方向垂直-->
<!--  android:layout_weight= "1"  代表所占权重(比例)    message占满其他元素剩余的地方,
         如果有多个android:layout_weight= 1 ,则平分剩余空间
          android:gravity= "top" 表示从上面开始占(写了android:layout_weight的)
          
          
            android:layout_gravity= "right"
            整个组件的位置居右
-->

wKiom1hIFsWCFl9PAABRkFm5EbE270.png-wh_50

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version= "1.0"  encoding= "utf-8" ?>
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
     android:layout_width= "fill_parent"
     android:layout_height= "fill_parent"
     android:paddingLeft= "16dp"
     android:paddingRight= "16dp"
     android:orientation= "horizontal"  >
     <EditText
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:hint= "@string/to"  />
     <EditText
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:hint= "@string/subject"  />
     <EditText
         android:layout_width= "wrap_content"
         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>

wKioL1hIFxSSehAQAAAj4zxp0jY739.png-wh_50

LinearLayout 是一个视图组,它所有的子视图都在一个方向对齐,水平或者垂直。你可以指定布局的方向通过 android:orientation 属性。

LinearLayout的所有子视图排列都是一个靠着另一个,因此垂直列表每行仅仅有一个子视图,不管有多宽。水平列表只能有一行的高度(最高子视图的高度加上边距距离)。LinearLayout 期望子视图之间都有margin,每个子视图都有gravity

线性布局支持给个别的子视图设定权重,通过android:layout_weight属性。就一个视图在屏幕上占多大的空间而言,这个属性给其设定了一个重要的值。一个大的权重值,允许它扩大到填充父视图中的任何剩余空间。子视图可以指定一个权重值,然后视图组剩余的其他的空间将会分配给其声明权重的子视图。默认的权重是0.

例如,如果有三个文本框,其中两个声明的权重为1,另外一个没有权重,没有权重第三个文本字段不会增加,只会占用其内容所需的面积。其他两个同样的会扩大以填补剩余的空间,在三个文本域被测量后。如果第三个字段,然后给定的权重为2(而不是0),那么它现在的声明比其他的更重要,所以它得到一半的总的剩余空间,而前两个平均分配剩余的。



 本文转自 matengbing 51CTO博客,原文链接:http://blog.51cto.com/matengbing/1880518

相关文章
|
28天前
|
XML Android开发 数据格式
android点击FrameLayout、LinearLayout等父布局没响应的原因以及解决方案
android点击FrameLayout、LinearLayout等父布局没响应的原因以及解决方案
27 2
|
28天前
|
Android开发
Android梅花布局
Android梅花布局
22 1
|
1月前
|
Android开发
android 布局常见调整手段,怎样查看margin,padding等等
android 布局常见调整手段,怎样查看margin,padding等等
16 0
|
6月前
|
XML 前端开发 Android开发
android 前端常用布局文件升级总结(二)
android 前端常用布局文件升级总结(二)
|
28天前
|
XML Android开发 数据格式
Android六大布局
Android六大布局
33 1
|
3月前
|
数据可视化 Android开发 容器
Android UI设计: 请解释LinearLayout、RelativeLayout和ConstraintLayout的区别。
Android UI设计: 请解释LinearLayout、RelativeLayout和ConstraintLayout的区别。
97 5
|
4月前
|
Android开发
Android Studio入门之常用布局的讲解以及实战(附源码 超详细必看)(包括线性布局、权重布局、相对布局、网格布局、滚动视图 )
Android Studio入门之常用布局的讲解以及实战(附源码 超详细必看)(包括线性布局、权重布局、相对布局、网格布局、滚动视图 )
140 0
|
22天前
|
XML 数据可视化 Java
Android布局——线性布局、相对布局、帧布局、表格布局、网格布局、约束布局
Android布局——线性布局、相对布局、帧布局、表格布局、网格布局、约束布局
|
22天前
|
数据可视化 Android开发
Android布局——约束布局
Android布局——约束布局
|
22天前
|
Android开发
Android布局——帧布局、表格布局、网格布局
Android布局——帧布局、表格布局、网格布局