zoukankan      html  css  js  c++  java
  • Android学习笔记_3_四种布局

    Android布局是应用界面开发的重要一环,在Android中,共有四种布局方式,

    分别是:FrameLayout( 帧布局 )、LinearLayout (线性布局)、TableLayout(表格布局)、RelativeLayout(相对布局)。

    1.LinearLayout

           线性布局LinearLayout 在单一方向上对齐所有的子视图-竖向或者横向,这依赖于你怎么定义方向orientation 属性。所有子视图依次堆积,所以一个竖向列表每行只有一个子视图,不管它们有多宽,而一个横向列表将只有一行高(最高子视图的高度,加上填充)。一个线性布局LinearLayout 会考虑子视图之间的边缘空白margins以及每个子视图的引力属性(靠右,居中,或者靠左)。

    2.RelativeLayout

           相对布局RelativeLayout允许子视图指定它们和父视图或彼此之间的相对位置(通过ID指定)。因此你可以按正确的顺序对齐两个元素,或者让一个视图在另外一个下面,居于屏幕中间,左边的中间,等等。元素通过给定顺序来绘制,因此如果这第一个元素在屏幕中间,其他以它对齐的元素都会对齐到屏幕中间。同样,因为这个顺序,如果使用XML来指定这个布局,你将引用的元素(为了定位其它视图对象)必须被列在XML文件中,在你通过引用ID从其他视图中引用它之前。

           其中一些特性直接由元素支持,另外一些由它的LayoutParams成员变量支持(为所有这个屏幕中的元素子类化RelativeLayout,因为所有元素都是RelativeLayout父对象的子元素)。已定义的相对布局RelativeLayout参数是:width,height,below,alignTop,toLeft,padding[Bottom|Left|Right|Top],以及 margin[Bottom|Left|Right|Top]。注意其中一些参数明确支持相对布局位置-它们的数值必须是你的相对位置元素的ID。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tvPhone"
            android:id="@+id/tv1" />
    
        <EditText
            android:id="@+id/editPh"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/tv1"
            android:layout_below="@+id/tv1"
            android:hint="@string/phoneMsg"
            android:ems="10" />    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/editPh"
            android:layout_below="@+id/editPh"
            android:text="@string/tvSMS" />
        <EditText
            android:id="@+id/etSMS"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_below="@+id/textView1"
            android:ems="10" />
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/etSMS"
            android:layout_below="@+id/etSMS"
            android:text="@string/send" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/button1"
            android:layout_alignBottom="@+id/button1"
            android:layout_alignRight="@+id/etSMS"
            android:text="Button" />
    
    </RelativeLayout>

    3.TableLayout

          表格布局TableLayout把它的子视图定位到行和列中。表格布局容器不显示行,列和单元的边界线。表的列和最多行单元数一样多。一个表可以有空单元,但是单元不能像HTML里面那样跨列。

           TableRow 对象是一个TableLayout的子视图(每个TableRow定义了表中的一个单独行)。每行有0或多个单元,可用任何其他视图定义。因此,行单元可能由各个视图对象组成,如ImageView或TextView对象。一个单元也可以是一个ViewGroup对象(比如,你可以嵌入另一个表布局作为一个单元)。

    <?xml version="1.0" encoding="utf-8"?>
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <TableRow 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            >   
          <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:height="60dp"
            android:gravity="center_vertical"
            android:text="@string/tvPhone"
            android:id="@+id/tv1" />
        </TableRow>
        <TableRow 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >   
         <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@string/tvPhone"
           android:id="@+id/tv2" />
       </TableRow>
    </TableLayout>


     

    4.FrameLayout

             FrameLayout是最简单的一个布局对象。它被定制为你屏幕上的一个空白备用区域,之后你可以在其中填充一个单一对象 — 比如,一张你要发布的图片。所有的子元素将会固定在屏幕的左上角;你不能为FrameLayout中的一个子元素指定一个位置。后一个子元素将会直接在前一个子元素之上进行覆盖填充,把它们部份或全部挡住(除非后一个子元素是透明的)。

    <?xml version="1.0" encoding="utf-8"?>
      <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent">
     
         <ImageView
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:src="@drawable/movie"
            android:contentDescription="@string/movie"/>
        <ImageView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:src="@drawable/paly"
            android:layout_gravity="center"
             android:contentDescription="@string/play"/>
    </FrameLayout>

    android:gravity设置空间中内容的对齐方式,android:layout_gravity设置控件本身的对齐方式

    当android:gravity和android:padding同时做用时,先按gravity进行排布,再按padding调整

     android:padding指定内边距,android:layout_margin指定外边距

  • 相关阅读:
    Windows 下ftp命令基本使用
    Oracle学习笔记:oracle和serverver在过程sql中通过select对变量进行赋值的区别
    分享最新36款高质量免费英文字体
    分享31个漂亮的矢量背景素材
    30个使用大自然元素设计的 Logo 欣赏
    分享5款精美的WordPress免费主题
    分享最新40个很不错的 PSD 资源
    WordPress精美免费主题分享系列之杂志风格篇
    分享25个很棒的网页设计教程和资源网站
    分享50个 CSS3 最佳应用示例
  • 原文地址:https://www.cnblogs.com/lbangel/p/3395406.html
Copyright © 2011-2022 走看看