初学Android,五大布局对象(六)

news/2024/7/4 23:10:32 标签: android, layout, encoding, button, blog, html
htmledit_views">
http://html" title=blog>blog.csdn.net/html" title=android>android_tutor/article/details/4779097

Android五大布局对象,它们分别是
FrameLayout(框架布局:不知道是不是这么翻译的),
LinearLayout (线性布局),
AbsoluteLayout(绝对布局),
RelativeLayout(相对布局),
TableLayout(表格布局).
FrameLayout:
FrameLayout是最简单的一个布局对象。它被定制为你屏幕上的一个空白备用区域,之后你可以在其中填充一个单一对象 — 比如,一张你要发布的图片。所有的子元素将会固定在屏幕的左上角;你不能为FrameLayout中的一个子元素指定一个位置。后一个子元素将会直接在前 一个子元素之上进行覆盖填充,把它们部份或全部挡住(除非后一个子元素是透明的)。
我们看一下效果图:
1.jpg
其中 Main.xml 代码如下:

  1. <?xml version="1.0" html" title=encoding>encoding="utf-8"?>
  2. <FrameLayout xmlns:html" title=android>android="http://schemas.html" title=android>android.com/apk/res/html" title=android>android" 
  3.     html" title=android>android:html" title=layout>layout_width="fill_parent"
  4.     html" title=android>android:html" title=layout>layout_height="fill_parent"
  5.     > <!-- 我们在这里加了一个Button按钮 --> 
  6. <Button 
  7.     html" title=android>android:text="html" title=button>button" 
  8.     html" title=android>android:html" title=layout>layout_width="fill_parent"
  9.     html" title=android>android:html" title=layout>layout_height="wrap_content"
  10. /> 
  11. <TextView 
  12.     html" title=android>android:text="textview"
  13.     html" title=android>android:textColor="#0000ff" 
  14.     html" title=android>android:html" title=layout>layout_width="wrap_content"
  15.     html" title=android>android:html" title=layout>layout_height="wrap_content"
  16. /> 
  17. </FrameLayout>
复制代码



LinearLayout:
LinearLayout以你为它设置的垂直或水平的属性值,来排列所有的子元素。所有的子元素都被堆放在其它元素之后,因此一个垂直列表的每一行只会有 一个元素,而不管他们有多宽,而一个水平列表将会只有一个行高(高度为最高子元素的高度加上边框高度)。LinearLayout保持子元素之间的间隔以 及互相对齐(相对一个元素的右对齐、中间对齐或者左对齐)。
LinearLayout还支持为单独的子元素指定 weight 。好处就是允许子元素可以填充屏幕上的剩余空间。这也避免了在一个大屏幕中,一串小对象挤 成一堆的情况,而是允许他们放大填充空白。子元素指定一个 weight 值,剩余的空间就会按这些子元素指定的 weight 比例分配给这些子元素。默认的  weight 值为0。例如,如果有三个文本框,其中两个指定了 weight 值为1,那么,这两个文本框将等比例地放大,并填满剩余的空间,而第三个文本框 不会放大。
我们看一下效果图:
2.jpg
其中 Main.xm l代码如下:

  1. <?xml version="1.0" html" title=encoding>encoding="utf-8"?>
  2. <LinearLayout xmlns:html" title=android>android="http://schemas.html" title=android>android.com/apk/res/html" title=android>android" 
  3.     html" title=android>android:orientation="vertical" 
  4.     html" title=android>android:html" title=layout>layout_width="fill_parent"
  5.     html" title=android>android:html" title=layout>layout_height="fill_parent">
  6.    <LinearLayout 
  7.     html" title=android>android:orientation="vertical" 
  8.     html" title=android>android:html" title=layout>layout_width="fill_parent"
  9.     html" title=android>android:html" title=layout>layout_height="fill_parent"
  10.     html" title=android>android:html" title=layout>layout_weight="2"> 
  11.     <TextView
  12.         html" title=android>android:text="Welcome to Mr Wei's html" title=blog>blog" 
  13.         html" title=android>android:textSize="15pt"
  14.         html" title=android>android:html" title=layout>layout_width="fill_parent"
  15.         html" title=android>android:html" title=layout>layout_height="wrap_content"
  16.      /> 
  17.     </LinearLayout> 
  18.     <LinearLayout 
  19.     html" title=android>android:orientation="horizontal" 
  20.     html" title=android>android:html" title=layout>layout_width="fill_parent"
  21.     html" title=android>android:html" title=layout>layout_height="fill_parent"
  22.     html" title=android>android:html" title=layout>layout_weight="1"> 
  23.     
  24.     <TextView 
  25.         html" title=android>android:text="red"        html" title=android>android:gravity="center_horizontal" //这里字水平居中
  26.         html" title=android>android:background="#aa0000"
  27.         html" title=android>android:html" title=layout>layout_width="wrap_content"
  28.         html" title=android>android:html" title=layout>layout_height="fill_parent"
  29.         html" title=android>android:html" title=layout>layout_weight="1"/> 
  30.     <TextView 
  31.         html" title=android>android:text="green"
  32.         html" title=android>android:gravity="center_horizontal "
  33.         html" title=android>android:background="#00aa00"
  34.         html" title=android>android:html" title=layout>layout_width="wrap_content"
  35.         html" title=android>android:html" title=layout>layout_height="fill_parent"
  36.         html" title=android>android:html" title=layout>layout_weight="1"/>    
  37.     </LinearLayout>
  38. </LinearLayout>
复制代码


AbsoluteLayout:
AbsoluteLayout 可以让子元素指定准确的x/y坐标值,并显示在屏幕上。(0, 0)为左上角,当向下或向右移动时,坐标值将变大。 AbsoluteLayout 没有页边框,允许元素之间互相重叠(尽管不推荐)。我们通常不推荐使用  AbsoluteLayout ,除非你有正当理由要使用它,因为它使界面代码太过刚性,以至于在不同的设备上可能不能很好地工作。
我们看一下效果图:
3.jpg
其中 Main.xm l代码如下:
  1. <?xml version="1.0" html" title=encoding>encoding="utf-8"?>
  2. <AbsoluteLayout xmlns:html" title=android>android="http://schemas.html" title=android>android.com/apk/res/html" title=android>android" 
  3.     html" title=android>android:orientation="vertical"
  4.     html" title=android>android:html" title=layout>layout_width="fill_parent"
  5.     html" title=android>android:html" title=layout>layout_height="fill_parent"
  6.     >
  7. <EditText 
  8.     html" title=android>android:text="Welcome to Mr Wei's html" title=blog>blog"
  9.     html" title=android>android:html" title=layout>layout_width="fill_parent" 
  10.     html" title=android>android:html" title=layout>layout_height="wrap_content" 
  11. /> 
  12. <Button 
  13.     html" title=android>android:html" title=layout>layout_x="250px" //设置按钮的X坐标
  14.     html" title=android>android:html" title=layout>layout_y="40px" //设置按钮的Y坐标
  15.     html" title=android>android:html" title=layout>layout_width="70px" //设置按钮的宽度
  16.     html" title=android>android:html" title=layout>layout_height="wrap_content"
  17.     html" title=android>android:text="Button" 
  18. /> 
  19. </AbsoluteLayout>
复制代码


RelativeLayout:
RelativeLayout 允许子元素指定他们相对于其它元素或父元素的位置(通过 ID 指定)。因此,你可以以右对齐,或上下,或置于屏幕中央的形式来 排列两个元素。元素按顺序排列,因此如果第一个元素在屏幕的中央,那么相对于这个元素的其它元素将以屏幕中央的相对位置来排列。如果使用 XML 来指定这个  html" title=layout>layout ,在你定义它之前,被关联的元素必须定义。
让我们看一下效果图:
4.jpg
其中 Main.xml 代码如下:
  1. <?xml version="1.0" html" title=encoding>encoding="utf-8"?>
  2. <RelativeLayout xmlns:html" title=android>android="http://schemas.html" title=android>android.com/apk/res/html" title=android>android" 
  3.     html" title=android>android:html" title=layout>layout_width="fill_parent"
  4.     html" title=android>android:html" title=layout>layout_height="fill_parent">
  5.     <TextView 
  6.         html" title=android>android:id="@+id/label" 
  7.         html" title=android>android:html" title=layout>layout_width="fill_parent"
  8.         html" title=android>android:html" title=layout>layout_height="wrap_content"
  9.         html" title=android>android:text="Welcome to Mr Wei's html" title=blog>blog:"/> 
  10.     <EditText 
  11.         html" title=android>android:id="@+id/entry" 
  12.         html" title=android>android:html" title=layout>layout_width="fill_parent"
  13.         html" title=android>android:html" title=layout>layout_height="wrap_content"
  14.         html" title=android>android:html" title=layout>layout_below="@id/label"/> 
  15.     <Button 
  16.         html" title=android>android:id="@+id/ok" 
  17.         html" title=android>android:html" title=layout>layout_width="wrap_content"
  18.         html" title=android>android:html" title=layout>layout_height="wrap_content"
  19.         html" title=android>android:html" title=layout>layout_below="@id/entry" 
  20.         html" title=android>android:html" title=layout>layout_alignParentRight="true"
  21.         html" title=android>android:html" title=layout>layout_marginLeft="10dip"
  22.         html" title=android>android:text="OK" /> 
  23.     <Button 
  24.         html" title=android>android:html" title=layout>layout_width="wrap_content"
  25.         html" title=android>android:html" title=layout>layout_height="wrap_content"
  26.         html" title=android>android:html" title=layout>layout_toLeftOf="@id/ok"
  27.         html" title=android>android:html" title=layout>layout_alignTop="@id/ok"
  28.         html" title=android>android:text="Cancel" /> 
  29. </RelativeLayout>
复制代码

TableLayout:
TableLayout 将子元素的位置分配到行或列中。一个 TableLayout 由许多的 TableRow 组成,每个 TableRow 都会定义一个 row (事实上,你可以定义其它的子对象,这在下面会解释到)。 TableLayout 容器不会显示 row 、 cloumns 或 cell 的边框线。每个  row 拥有0个或多个的 cell ;每个 cell 拥有一个 View 对象。表格由列和行组成许多的单元格。表格允许单元格为空。单元格不能跨列,这与 HTML 中的不一样。
下面让我们看一下效果图:
5.jpg
其中 Main.xml 代码如下:
  1. <?xml version="1.0" html" title=encoding>encoding="utf-8"?>
  2. <TableLayout xmlns:html" title=android>android="http://schemas.html" title=android>android.com/apk/res/html" title=android>android" 
  3.     html" title=android>android:html" title=layout>layout_width="fill_parent" html" title=android>android:html" title=layout>layout_height="fill_parent"
  4.     html" title=android>android:stretchColumns="1">
  5.     <TableRow> 
  6.         <TextView html" title=android>android:html" title=layout>layout_column="1" html" title=android>android:text="Open..." />
  7.         <TextView html" title=android>android:text="Ctrl-O" html" title=android>android:gravity="right" />
  8.     </TableRow> 
  9.     <TableRow> 
  10.         <TextView html" title=android>android:html" title=layout>layout_column="1" html" title=android>android:text="Save..." />
  11.         <TextView html" title=android>android:text="Ctrl-S" html" title=android>android:gravity="right" />
  12.     </TableRow> 
  13.     <View html" title=android>android:html" title=layout>layout_height="2dip" html" title=android>android:background="#FF909090" /> //这里是上图中的分隔线
  14.     <TableRow> 
  15.         <TextView html" title=android>android:text="X" />
  16.         <TextView html" title=android>android:text="Export..." />
  17.         <TextView html" title=android>android:text="Ctrl-E" html" title=android>android:gravity="right " />
  18.     </TableRow> 
  19.     <View html" title=android>android:html" title=layout>layout_height="2dip" html" title=android>android:background="#FF909090" /> 
  20.     <TableRow> 
  21.         <TextView html" title=android>android:html" title=layout>layout_column="1" html" title=android>android:text="Quit"
  22.             html" title=android>android:padding="3dip" />
  23.     </TableRow> 
  24. </TableLayout>
复制代码


http://www.niftyadmin.cn/n/1411436.html

相关文章

TypeScript 安装与使用

2019独角兽企业重金招聘Python工程师标准>>> 安装TypeScript sudo npm install -g typescript编写第一个ts /*** Created by yanhao on 2017/5/25.*/ let user:string "uwo"; console.log(user);生成js命令 tsc index.ts结果 /*** Created by yanhao on …

初学Android,启动,关闭Activity(七)

Acitivity启动其他Activity有如下两种方法 startActivity(Intent intent): 启动其它Activity startActivityForResult(Intent intent,int requestCode): 以指请求码启动Activity,而且程序将会得到新启动Activity的结果(重写onActivityResult(...)方法获取) 相对的,也有两个关闭…

自动化测试之selenium API 查找元素操作底层方法

搜罗了一些查找元素的除标准语句外&#xff0c;另外的语句使用方法&#xff0c;摘自 开源中国 郝云鹏driver webdriver.Chrome(); 打开测试页面 driver.get( "http://baidu.com" ); ---------------页面元素定位(页面元素)-------------- 通过ID进行定位 <div id…

初学Android,使用Bundle在Activity间交换数据(八)

在Android中,两个Acitivity之间是靠Intent传递信息的,因为Intent本来就起到信使的作用,所以用它来传递数据也显得顺理成章了. Intent 提供了多个方法来"携带"额外的数据 putExtras(Bundle data): 向Intent中放入需要"携带"的数据 putXxx(String key,Xxx da…

设计模式——Constructor(构造器)模式

JavaScript不支持类的概念&#xff0c;但是可以用 function 进行模拟&#xff0c;也就是通过在构造器函数面前加 new 关键字&#xff0c;实例化一个对象。 在构造器内部&#xff0c;this 关键字引用新创建的对象。 一个基本的构造器看起来像这样。 function Cat(name, age){…

初学Android,使用Bundle在Activity间交换数据(九)

在上一篇里面写了Activity之间的传值&#xff0c;这一篇里面写写怎么启动一个新的Activity之后&#xff0c;得到它的返回值 获得返回值需要重写当前Activity的onActivityResult(int requestCode,int resultCode,Intent intent),requestCode代表请求码,而resultCode代表结果码 下…

CSS3颜色

1.设置半透明&#xff1a;color&#xff1a;rgba(R,G,B,A)&#xff0c;A为透明程度&#xff0c;在0-1之间 2.渐变色彩&#xff1a;线性渐变&#xff1a;background-image:linear-gradient(to left top,red,white,black); 转载于:https://www.cnblogs.com/SunnyYYN/p/6916736.ht…

设计模式——Module(模块)模式

Module的实现方式 在JavaScript中&#xff0c;有这样几种实现模块的方式。 对象的字面量表示法Module模式AMD模块CommonJS模块ECMAScript Harmony模块 针对这些方式&#xff0c;Module&#xff08;模块&#xff09;模式&#xff0c;作为设计模式的一种&#xff0c;需要研究。…