android 五大布局(1)线性布局

来源:互联网 发布:2017年时代网络诗人奖 编辑:程序博客网 时间:2024/06/03 03:30
Layout
Layout(布局)是ViewGroup的实现类(即子类),为视图控件提供排列结构。
常用的布局:
FrameLayout(帧布局)
LinearLayout(线性布局)
TableLayout(表格布局)
RelativeLayout(相对布局)
AbsoluteLayout(绝对布局)
布局参数:
布局参数定义控件的位置、尺寸等属性。
控件的位置由视图的左上点坐标、对齐方式等属性确定。

控件的尺寸由视图的宽度、高度等属性确定。



LinearLayout包含的子控件将以横向或竖向的方式排列。超过边界时,某些控件将缺失或是消失。


线性布局(LinearLayout)是一种重要的界面布局中,也是经常使用到的一种界面布局
在线性布局中,所有的子元素都按照垂直或水平的顺序在界面上排列,不会换行
如果垂直排列,则每行仅包含一个界面元素
如果水平排列,则每列仅包含一个界面元素
属性android:orientation
Horizontal


vertical


在线性布局中,有个非常重要的属性gravity,这个属性用来指定组件内容的对齐方式
gravity的中文意思就是”重心“,就是表示view横向和纵向的停靠位置 ,支持top、bottom、left、right、center_vertical、 fill_vertical、center_horizontal、fill_horizontal、center、fill、 clip_vertical、clip_horizontal几个属性值。
可以同时指定多种对齐方式,如left|center_vertical表示出 现在屏幕左边,并且垂直居中 

<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="bottom|center_horizontal">
</LinearLayout>




在线性布局中,LinearLayout有两个非常相似的属性:android:gravity与android:layout_gravity 
android:gravity:是对view控件本身来说的,是用来设置view本身的内容应该显示在view的什么位置,默认值是左侧 。
android:layout_gravity:是相对于包含该元素的父元素来说的,设置该元素在父元素的什么位置 

<Button android:id="@+id/btn1" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="gravity测试"
android:gravity="right" />
<Button android:id="@+id/btn2" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="layout_gravity测试"
android:layout_gravity="right" />



在线性布局中,LinearLayout还支持为其包含的widget或者是container指定填充权值layout_weight 
默认的 weight 值为0 ,表示按照widgets或者是containers实际大小来显示,若高于0的值,则将 Container剩余可用空间分割,分割大小具体取决于每一个widget或者是 container的layout_weight及该权值在所有widgets或者是containers中的比例 

<Button android:id="@+id/btn1" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="测试按钮1" 
android:layout_weight="3"/>
<Button android:id="@+id/btn2" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="测试按钮2" 
android:layout_weight="2"/>
<Button android:id="@+id/btn3" android:layout_width="fill_parent"




动态创建布局 
在将每一个View加入到这个Layout里边的时候,我们会传传递一组值,这组值封装在LayoutParams这个类当中。在显示这个View的时候,它的容器会根据传进来的LayoutParams进行计算,来确认这个View显示的大小和位置
layout_width - 宽
layout_height - 高

/ 定义布局管理器的指定宽和高
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);


layout.setOrientation(LinearLayout.VERTICAL);




一个LinearLayout嵌套两个LinearLayout,外层的LinearLayout用纵向布局,而内部的用横向布局。 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal">……
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal">……
</LinearLayout>
</LinearLayout>



阅读全文
0 0
原创粉丝点击