Android基础布局之六大布局

来源:互联网 发布:isis软件安装 编辑:程序博客网 时间:2024/06/06 03:43

原创作品允许转载   转载前请注明出处 !

 

明天的文盲将是那些不主动追求知识的人!!!

 

布局管理器都是以ViewGroup为基类派生出来的; 使用布局管理器可以适配不同手机屏幕的分辨率,尺寸大小;

 

 

如何使用XML文件定义视图:

 

     每个Android项目的源码目录下都有个res/layout目录,这个目录就是用来存放布局文件的。布局文件一般以对应

activity的名字命名,以 .xml 为后缀。在xml中为创建组件时,需要为组件指定id,如:android:id="@+id/名字"系统会自动

在gen目录下创建相应的R资源类变量。

 

    如何在代码中使用视图: 

 

     在代码中创建每个Activity时,一般是在onCreate()方法中,调用setContentView()来加载指定的xml布局文件,然后就

可以通过findViewById()来获得在布局文件中创建的相应id的控件了,如Button等。

 

 如:

 

 

private Button btnSndMag; 

public void onCreate(BundlesavedInstanceState) { 

    super.onCreate(savedInstanceState); 

   setContentView(R.layout.main);  //加载main.xml布局文件 

   btnSndMag = (Button)this.findViewById(R.id.btnSndMag); // 通过id找到对于的Button组件 

   .... 

 

  下面我们来介绍Android系统中为我们提供的五大布局:LinearLayout(线性布局)、FrameLayout(单帧布局)、

AbsoluteLayout(绝对布局)、TablelLayout(表格布局)、RelativeLayout(相对布局)。其中最常用的的是LinearLayout、

TablelLayout和RelativeLayout。这些布局都可以嵌套使用。

 

 

(1)LinearLayout 线性布局

 

  线性布局是按照水平或垂直的顺序将子元素(可以是控件或布局)依次按照顺序排列,每一个元素都位于前面一个元素之后。线性布局分为两种:水平方向和垂直方向的布局。分别通过属性android:orientation="vertical" (垂直)和 android:orientation="horizontal"(水平)来设置。默认是按照水平进行排布。

(1)基线对齐

 

xml属性 : android:baselineAligned; 

设置方法 : setBaselineAligned(booleanb); 

作用 : 如果该属性为false, 就会阻止该布局管理器与其子元素的基线对齐;

 

(2)设分隔条 

 

xml属性 : android:divider; 

设置方法 : setDividerDrawable(Drawable); 

作用 : 设置垂直布局时两个按钮之间的分隔条;

 

(3)对齐方式(控制内部子元素) 

 

xml属性 : android:gravity; 

设置方法 : setGravity(int); 

作用 : 设置布局管理器内组件(子元素)的对齐方式, 表示内部内容的排布方式。加在布局中表示布局内部控件的排布方式。加在控件上表示控件的内部内容相对于控件自身的排布方式。

支持的属性 : 

top, bottom,left, right, 

center_vertical(垂直方向居中), center_horizontal(水平方向居中),

fill_vertical(垂直方向拉伸), fill_horizontal(水平方向拉伸), 

center,fill, 

clip_vertical,clip_horizontal; 

可以同时指定多种对齐方式 : 如left|center_vertical 左侧垂直居中;

layout_gravity:表示控件相对于父类布局的排布方式。

注意特点:如果在线性布局整体是水平排布的时候,layout_gravity只在垂直方向上有作用。同样反之亦然。

 

margin:当前控件相对于其他的控件的距离。相当于距离上下左右。

marginLeft:距离左边有多少间距。

 

 

android:layout_weight (权重)表示子元素占据的空间大小的比例。----只存在于线性布局

如果划分的方向是使用的是wrap_content,那么权重越大,所占的比值越大,

如果是match_parent。那么权重越大,所占的比值越小。

weight_sum:可以指定当前划分的权重的总份数。

 

android:layout_weight的真实含义是:一旦View设置了该属性(假设有效的情况下),那么该 View的宽度等于原有宽度(android:layout_width)加上剩余空间的占比!

设屏幕宽度为L,在两个view的宽度都为match_parent的情况下,原有宽度为L,两个的View的宽度都为L,那么剩余宽度为L-(L+L) = -L, 左边的View占比三分之一,所以总宽度是L+(-L)*1/3 = (2/3)L.事实上默认的View的weight这个值为0,一旦设置了这个值,那么所在view在绘制的时候执行onMeasure两次的原因就在这。

Google官方推荐,当使用weight属性时,将width设为0dip即可,效果跟设成wrap_content是一样的。这样weight就可以理解为占比了!

 

思考:有权重的控件和无权重的控件,系统先给谁分配空间?

答:先分配给无权重的控件,减去无权重的长度剩下的按比例分配给有权重的空间!

 

 

实例:

 



<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_weight="1" >        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="button" /><View    android:layout_width="0dp"    android:layout_height="0dp"    android:layout_weight="1"    />        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="button" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_weight="1"        android:gravity="center" >        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="button" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_weight="1"        android:gravity="bottom" >        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="button" /><View    android:layout_width="0dp"    android:layout_height="0dp"    android:layout_weight="1"    />        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="button" />    </LinearLayout></LinearLayout>


 

(2)TableLayout 表格布局

         表格布局,适用于多行多列的布局格式,每个TableLayout是由多个TableRow组成,一个TableRow就表示TableLayout中的每一行,这一行可以由多个子元素组成。实际上TableLayout和TableRow都是LineLayout线性布局的子类。但是TableRow的参数android:orientation属性值固定为horizontal,且android:layout_width=MATCH_PARENT,android:layout_height=WRAP_CONTENT。所以TableRow实际是一个横向的线性布局,且所以子元素宽度和高度一致。

       注意:在TableLayout中,单元格可以为空,但是不能跨列,意思是只能不能有相邻的单元格为空。

                     当一个空间跟tablerow同级别时 充当的也是tablerow一行

        在TableLayout布局中,一列的宽度由该列中最宽的那个单元格指定,而该表格的宽度由父容器指定。可以为每一列设置以下属性:

     Shrinkable  表示该列的宽度可以进行收缩,以使表格能够适应父容器的大小

     Stretchable 表示该列的宽度可以进行拉伸,以使能够填满表格中的空闲空间

     Collapsed  表示该列会被隐藏

TableLayout中的特有属性:

android:collapseColumns:隐藏指定的列
android:shrinkColumns:收缩指定的列以适合屏幕,不会挤出屏幕
android:stretchColumns:尽量把指定的列填充空白部分
android:layout_column:控件放在指定的列
android:layout_span:该控件所跨越的列数

android:collapseColumns

        android:shrinkColumns

        android:stretchColumns ="0,1,2,3"// 表示产生4个可拉伸的列

Demo:我们想设计一个如下所以的一个三行三列的表格,但是第二行我们只想显示2个表格:

 

 


  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:shrinkColumns="0,1,2"  // 设置三列都可以收缩    
  5.     android:stretchColumns="0,1,2" // 设置三列都可以拉伸 如果不设置这个,那个显示的表格将不能填慢整个屏幕  
  6.     android:layout_width="fill_parent"  
  7.     android:layout_height="fill_parent" >      
  8.     <TableRow android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content">  
  10.         <Button android:gravity="center"  
  11.             android:padding="10dp"  
  12.             android:text="Button1">  
  13.         </Button>  
  14.         
  15.         <Button android:gravity="center"  
  16.             android:padding="10dp"  
  17.             android:text="Button2">  
  18.         </Button>  
  19.         <Button android:gravity="center"  
  20.             android:padding="10dp"  
  21.             android:text="Button3">  
  22.         </Button>  
  23.     </TableRow>  
  24.     <TableRow android:layout_width="fill_parent"  
  25.         android:layout_height="wrap_content">  
  26.         <Button android:gravity="center"  
  27.             android:padding="10dp"  
  28.             android:text="Button4">  
  29.         </Button>  
  30.         
  31.         <Button android:gravity="center"  
  32.             android:padding="10dp"  
  33.             android:text="Button5">  
  34.         </Button>  
  35.     </TableRow>  
  36.     <TableRow android:layout_width="fill_parent"  
  37.         android:layout_height="wrap_content">  
  38.         <Button android:gravity="center"  
  39.             android:padding="10dp"  
  40.             android:text="Button6">  
  41.         </Button>       
  42.         <Button android:gravity="center"  
  43.             android:padding="10dp"  
  44.             android:text="Button7">  
  45.         </Button>  
  46.         <Button android:gravity="center"  
  47.             android:padding="10dp"  
  48.             android:text="Button8">  
  49.         </Button>  
  50.     </TableRow>  
  51. </TableLayout>  

 

(4)FrameLayout 框架布局(即帧布局)

 使用左侧滑动菜单栏

简单的例子

效果图:

 

 核心代码:

main.xml

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

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

3.           android:layout_width="fill_parent" 

4.           android:layout_height="fill_parent" 

5.           > 

6.           <TextView    

7.               android:layout_width="300dp"  

8.               android:layout_height="300dp"  

9.               android:background="#00BFFF"         

10.           /> 

11.       <TextView    

12.           android:layout_width="260dp"  

13.           android:layout_height="260dp"  

14.           android:background="#FFC0CB"         

15.           /> 

16.       <TextView    

17.           android:layout_width="220dp"  

18.           android:layout_height="220dp"  

19.           android:background="#0000FF"         

20.           /> 

21.   </FrameLayout> 

22.  4.相对布局

23.  相对布局是按照组件之间的相对位置来布局,比如在某个组件的左边,右边,上面和下面等,父控件和子控件均可

24.  RelativeLayout用到的一些重要的属性: 

25.  第一类:属性值为true或false

26.  android:layout_centerHrizontal 水平居中 
android:layout_centerVertical   垂直居中 
android:layout_centerInparent    相对于父元素完全居中 
android:layout_alignParentBottom 贴紧父元素的下边缘 
android:layout_alignParentLeft   贴紧父元素的左边缘 
android:layout_alignParentRight  贴紧父元素的右边缘 
android:layout_alignParentTop    贴紧父元素的上边缘 
android:layout_alignWithParentIfMissing  如果对应的兄弟元素找不到的话就以父元素做参照物

27.  第二类:属性值必须为id的引用名“@id/id-name”
 
android:layout_below      在某元素的下方 
android:layout_above      在某元素的的上方 
android:layout_toLeftOf   在某元素的左边 
android:layout_toRightOf  在某元素的右边 
android:layout_alignTop   本元素的上边缘和某元素的的上边缘对齐 
android:layout_alignLeft  本元素的左边缘和某元素的的左边缘对齐 
android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐 
android:layout_alignRight  本元素的右边缘和某元素的的右边缘对齐

28.  第三类:属性值为具体的像素值,如30dip,40px

29.  android:layout_marginBottom             离某元素底边缘的距离 
android:layout_marginLeft                  离某元素左边缘的距离 
android:layout_marginRight                离某元素右边缘的距离 
android:layout_marginTop  

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

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"

   android:layout_width="fill_parent"android:layout_height="fill_parent">

    <Button

         android:id="@+id/btnmiddle"

         android:text="MiddleButton"

         android:layout_width="200px"

         android:layout_height="wrap_content"

         android:layout_centerInParent="true">    

    </Button>

     <Button

         android:id="@+id/btnup"

         android:text="UpButton"

          android:layout_width="100px"

         android:layout_height="wrap_content"         

         android:layout_above="@id/btnmiddle"

         android:layout_alignLeft="@id/btnmiddle">    

    </Button>

     <Button

         android:id="@+id/btndown"

         android:text="downButton"

         android:layout_width="100px"

         android:layout_height="wrap_content"         

         android:layout_below="@id/btnmiddle"

         android:layout_alignRight="@id/btnmiddle">    

    </Button>

</RelativeLayout>

(5) AbsoluteLayou 绝对布局

   绝对布局中将所有的子元素通过设置android:layout_x和 android:layout_y属性,将子元素的坐标位置固定下来,即坐标(android:layout_x, android:layout_y) ,layout_x用来表示横坐标,layout_y用来表示纵坐标。屏幕左上角为坐标(0,0),横向往右为正方,纵向往下为正方。实际应用中,这种布局用的比较少,因为Android终端一般机型比较多,各自的屏幕大小。分辨率等可能都不一样,如果用绝对布局,可能导致在有的终端上显示不全等。

 

除上面讲过之外常用的几个布局的属性:
(1)layout_margin 
用于设置控件边缘相对于父控件的边距
android:layout_marginLeft 
android:layout_marginRight
android:layout_marginTop
android:layout_marginBottom


(2) layout_padding 
用于设置控件内容相对于控件边缘的边距
android:layout_paddingLeft
android:layout_paddingRight
android:layout_paddingTop
android:layout_paddingBottom


(3) layout_width/height
用于设置控件的高度和宽度
wrap_content 内容包裹,表示这个控件的里面文字大小填充
fill_parent 跟随父窗口
match_parent


(4) gravity 
用于设置View组件里面内容的对齐方式
top bottom  left   right   center等


(5) android:layout_gravity  
用于设置Container组件的对齐方式
android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐
android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐
android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐
android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐

更多参考:

线性布局

http://blog.csdn.net/loongembedded/article/details/32920759

帧布局

http://blog.csdn.net/loongembedded/article/details/40682827

表格布局

http://blog.csdn.net/loongembedded/article/details/40678397

相对布局

http://blog.csdn.net/loongembedded/article/details/35569043

 

2 0
原创粉丝点击