ZNDS智能电视网 推荐当贝市场

TV应用下载 / 资源分享区

软件下载 | 游戏 | 讨论 | 电视计算器

综合交流 / 评测 / 活动区

交流区 | 测硬件 | 网站活动 | Z币中心

新手入门 / 进阶 / 社区互助

新手 | 你问我答 | 免费刷机救砖 | ROM固件

查看: 12831|回复: 0
上一主题 下一主题
[教程]

Android基础教程(二)之五大布局对象

[复制链接]
跳转到指定楼层
楼主
发表于 2013-8-28 16:28 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
>
大家好,我们这一节讲一下Android对用五大布局对象,它们分别是FrameLayout(框架布局:不知道是不是这么翻译的),LinearLayout (线性布局),AbsoluteLayout(绝对布局),RelativeLayout(相对布局),TableLayout(表格布局).
FrameLayout是最简单的一个布局对象。它被定制为你屏幕上的一个空白备用区域,之后你可以在其中填充一个单一对象 — 比如,一张你要发布的图片。所有的子元素将会固定在屏幕的左上角;你不能为FrameLayout中的一个子元素指定一个位置。后一个子元素将会直接在前 一个子元素之上进行覆盖填充,把它们部份或全部挡住(除非后一个子元素是透明的)。
我们看一下效果图:
  
其中 代码如下:
   
   
  1. <?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"   
        > <!-- 我们在这里加了一个Button按钮 -->   
    <Button   
        android:text="button"   
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content"   
    />   
    <TextView   
        android:text="textview"   
        android:textColor="#0000ff"   
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content"   
    />   
    </FrameLayout>
复制代码
  
   
   
LinearLayout以你为它设置的垂直或水平的属性值,来排列所有的子元素。所有的子元素都被堆放在其它元素之后,因此一个垂直列表的每一行只会有 一个元素,而不管他们有多宽,而一个水平列表将会只有一个行高(高度为最高子元素的高度加上边框高度)。LinearLayout保持子元素之间的间隔以 及互相对齐(相对一个元素的右对齐、中间对齐或者左对齐)。
LinearLayout还支持为单独的子元素指定 。好处就是允许子元素可以填充屏幕上的剩余空间。这也避免了在一个大屏幕中,一串小对象挤 成一堆的情况,而是允许他们放大填充空白。子元素指定一个 值,剩余的空间就会按这些子元素指定的 比例分配给这些子元素。默认的  值为0。例如,如果有三个文本框,其中两个指定了 值为1,那么,这两个文本框将等比例地放大,并填满剩余的空间,而第三个文本框 不会放大。
我们看一下效果图:
  
其中 l代码如下:
   
   
  1. <?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">   
       <LinearLayout   
        android:orientation="vertical"   
        android:layout_width="fill_parent"   
        android:layout_height="fill_parent"   
        android:layout_weight="2">   
        <TextView   
            android:text="Welcome to Mr Weis blog"   
            android:textSize="15pt"   
            android:layout_width="fill_parent"   
            android:layout_height="wrap_content"   
         />   
        </LinearLayout>   
        <LinearLayout   
        android:orientation="horizontal"   
        android:layout_width="fill_parent"   
        android:layout_height="fill_parent"   
        android:layout_weight="1">   
          
        <TextView   
            android:text="red"        android:gravity="center_horizontal" //这里字水平居中   
            android:background="#aa0000"   
            android:layout_width="wrap_content"   
            android:layout_height="fill_parent"   
            android:layout_weight="1"/>   
        <TextView   
            android:text="green"   
            android:gravity="center_horizontal "   
            android:background="#00aa00"   
            android:layout_width="wrap_content"   
            android:layout_height="fill_parent"   
            android:layout_weight="1"/>      
        </LinearLayout>   
    </LinearLayout>
复制代码
  
   
可以让子元素指定准确的x/y坐标值,并显示在屏幕上。(0, 0)为左上角,当向下或向右移动时,坐标值将变大。 没有页边框,允许元素之间互相重叠(尽管不推荐)。我们通常不推荐使用  ,除非你有正当理由要使用它,因为它使界面代码太过刚性,以至于在不同的设备上可能不能很好地工作。
我们看一下效果图:
  
其中 l代码如下:
   
  1. <?xml version="1.0" encoding="utf-8"?>   
    <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"   
        android:orientation="vertical"   
        android:layout_width="fill_parent"   
        android:layout_height="fill_parent"   
        >   
    <EditText   
        android:text="Welcome to Mr Weis blog"   
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content"   
    />   
    <Button   
        android:layout_x="250px" //设置按钮的X坐标   
        android:layout_y="40px" //设置按钮的Y坐标   
        android:layout_width="70px" //设置按钮的宽度   
        android:layout_height="wrap_content"   
        android:text="Button"   
    />   
    </AbsoluteLayout>
复制代码
      
允许子元素指定他们相对于其它元素或父元素的位置(通过 指定)。因此,你可以以右对齐,或上下,或置于屏幕中央的形式来 排列两个元素。元素按顺序排列,因此如果第一个元素在屏幕的中央,那么相对于这个元素的其它元素将以屏幕中央的相对位置来排列。如果使用 来指定这个 ,在你定义它之前,被关联的元素必须定义。
让我们看一下效果图:
  
其中 代码如下:
   
  1. <?xml version="1.0" encoding="utf-8"?>   
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   
        android:layout_width="fill_parent"   
        android:layout_height="fill_parent">   
        <TextView   
            android:id="@+id/label"   
            android:layout_width="fill_parent"   
            android:layout_height="wrap_content"   
            android:text="Welcome to Mr Weis blog:"/>   
        <EditText   
            android:id="@+id/entry"   
            android:layout_width="fill_parent"   
            android:layout_height="wrap_content"   
            android:layout_below="@id/label"/>   
        <Button   
            android:id="@+id/ok"   
            android:layout_width="wrap_content"   
            android:layout_height="wrap_content"   
            android:layout_below="@id/entry"   
            android:layout_alignParentRight="true"   
            android:layout_marginLeft="10dip"   
            android:text="OK" />   
        <Button   
            android:layout_width="wrap_content"   
            android:layout_height="wrap_content"   
            android:layout_toLeftOf="@id/ok"   
            android:layout_alignTop="@id/ok"   
            android:text="Cancel" />   
    </RelativeLayout>
复制代码
   
将子元素的位置分配到行或列中。一个 由许多的 组成,每个 都会定义一个  (事实上,你可以定义其它的子对象,这在下面会解释到)。 容器不会显示 、 或 的边框线。每个  拥有0个或多个的 ;每个 拥有一个 对象。表格由列和行组成许多的单元格。表格允许单元格为空。单元格不能跨列,这与中的不一样。
下面让我们看一下效果图:
  
其中 代码如下:
   
  1. <?xml version="1.0" encoding="utf-8"?>   
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"   
        android:layout_width="fill_parent" android:layout_height="fill_parent"   
        android:stretchColumns="1">   
        <TableRow>   
            <TextView android:layout_column="1" android:text="Open..." />   
            <TextView android:text="Ctrl-O" android:gravity="right" />   
        </TableRow>   
        <TableRow>   
            <TextView android:layout_column="1" android:text="Save..." />   
            <TextView android:text="Ctrl-S" android:gravity="right" />   
        </TableRow>   
        <View android:layout_height="2dip" android:background="#FF909090" /> //这里是上图中的分隔线   
        <TableRow>   
            <TextView android:text="X" />   
            <TextView android:text="Export..." />   
            <TextView android:text="Ctrl-E" android:gravity="right " />   
        </TableRow>   
        <View android:layout_height="2dip" android:background="#FF909090" />   
        <TableRow>   
            <TextView android:layout_column="1" android:text="Quit"   
                android:padding="3dip" />   
        </TableRow>   
    </TableLayout>
复制代码
   以上就是Android五大布局对象,这个工程好庞大,足足花了我一个多小时的时间,希望对大家有帮助~大家多留言!</div

上一篇:Android基础教程(四)之-----取得手机屏幕大小DisplayMetrics的应用
下一篇:android项目清单介绍
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐 上一条 /1 下一条

Archiver|新帖|标签|软件|Sitemap|ZNDS智能电视网 ( 苏ICP备2023012627号 )

网络信息服务信用承诺书 | 增值电信业务经营许可证:苏B2-20221768 丨 苏公网安备 32011402011373号

GMT+8, 2025-7-29 13:12 , Processed in 0.058952 second(s), 16 queries , Redis On.

Powered by Discuz!

监督举报:report#znds.com (请将#替换为@)

© 2007-2025 ZNDS.Com

快速回复 返回顶部 返回列表