zoukankan      html  css  js  c++  java
  • Android代码常用布局

    1、线性布局 LinearLayout:
           线性布局是所有布局中最常用的类之一,也是RadioGroup, TabWidget, TableLayout, TableRow, ZoomControls类的父类。LinearLayout可以让它的子元素垂直或水平的方式排成一行(不设置方向的时候默认按照垂直方向排列)。 
    举个例子:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3 android:orientation="vertical"
     4 android:layout_width="fill_parent"
     5 android:layout_height="wrap_content"
     6 >
     7 <!--
     8 android:id —— 为控件指定相应的ID
     9 android:text —— 指定控件当中显示的文字,需要注意的是,这里尽量使用strings.xml文件当中的字符串
    10 android:grivity —— 指定控件的基本位置,比如说居中,居右等位置
    11 android:textSize —— 指定控件当中字体的大小
    12 android:background —— 指定该控件所使用的背景色,RGB命名法 
    13 android:width —— 指定控件的宽度
    14 android:height —— 指定控件的高度
    15 android:padding* —— 指定控件的内边距,也就是说控件当中的内容
    16 android:layout_weight —— 控件之间的权重比
    17 android:sigleLine —— 如果设置为真的话,则将控件的内容在同一行当中进行显示
    18 -->
    19 <TextView
    20 android:id="@+id/firstText"
    21 android:text="第一行一行一行一行一行一行一行一行一行一行"
    22 android:gravity="center_vertical"
    23 android:textSize="35pt"
    24 android:background="#aa0000"
    25 android:layout_width="fill_parent"
    26 android:layout_height="wrap_content"
    27 android:paddingLeft="10dip"
    28 android:paddingTop="20dip"
    29 android:paddingRight="30dip"
    30 android:paddingBottom="40dip"
    31 android:layout_weight="1"
    32 android:singleLine="true"/>
    33 <TextView
    34 android:id="@+id/secondText"
    35 android:text="第二行"
    36 android:gravity="center_vertical"
    37 android:textSize="15pt"
    38 android:background="#0000aa"
    39 android:layout_width="fill_parent"
    40 android:layout_height="wrap_content"
    41 android:layout_weight="1"/>
    42 </LinearLayout>

       2、相对布局 RelativeLayout 
           相对布局 RelativeLayout 允许子元素指定它们相对于其父元素或兄弟元素的位置,这是实际布局中最常用的布局方式之一。它灵活性大很多,当然属性也多,操作难度也大,属性之间产生冲突的的可能性也大,使用相对布局时要多做些测试。 

    举个例子: 

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <!--
     3 android:layout_above 将该控件的底部至于给定ID的控件之上
     4 android:layout_below 将该控件的顶部至于给定ID的控件之下
     5 android:layout_toLeftOf 将该控件的右边缘和给定ID的控件的左边缘对齐
     6 android:layout_toRightOf 将该控件的左边缘和给定ID的控件的右边缘对齐
     7 
     8 android:layout_alignBaseline 该控件的baseline和给定ID的控件的baseline对齐
     9 android:layout_alignBottom 将该控件的底部边缘与给定ID控件的底部边缘
    10 android:layout_alignLeft 将该控件的左边缘与给定ID控件的左边缘对齐
    11 android:layout_alignRight 将该控件的右边缘与给定ID控件的右边缘对齐
    12 android:layout_alignTop 将给定控件的顶部边缘与给定ID控件的顶部对齐
    13 
    14 
    15 android:alignParentBottom 如果该值为true,则将该控件的底部和父控件的底部对齐
    16 android:layout_alignParentLeft 如果该值为true,则将该控件的左边与父控件的左边对齐
    17 android:layout_alignParentRight 如果该值为true,则将该控件的右边与父控件的右边对齐
    18 android:layout_alignParentTop 如果该值为true,则将空间的顶部与父控件的顶部对齐
    19 
    20 android:layout_centerHorizontal 如果值为真,该控件将被至于水平方向的中央
    21 android:layout_centerInParent 如果值为真,该控件将被至于父控件水平方向和垂直方向的中央
    22 android:layout_centerVertical 如果值为真,该控件将被至于垂直方向的中央
    23 -->
    24 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    25 android:layout_width="fill_parent" 
    26 android:layout_height="fill_parent">
    27 <TextView 
    28 android:id="@+id/label" 
    29 android:layout_width="fill_parent"
    30 android:layout_height="wrap_content" 
    31 android:text="Type here:" />
    32 <EditText 
    33 android:id="@+id/entry" 
    34 android:layout_width="fill_parent"
    35 android:layout_height="wrap_content" 
    36 android:background="@android:drawable/editbox_background"
    37 android:layout_below="@id/label" />
    38 <Button android:id="@+id/ok" 
    39 android:layout_width="wrap_content"
    40 android:layout_height="wrap_content" 
    41 android:layout_below="@id/entry"
    42 android:layout_alignParentRight="true" 
    43 android:layout_marginLeft="10dip"
    44 android:text="OK" />
    45 <Button android:layout_width="wrap_content"
    46 android:layout_height="wrap_content" 
    47 android:layout_toLeftOf="@id/ok"
    48 android:layout_alignTop="@id/ok" 
    49 android:text="Cancel" />
    50 </RelativeLayout>

    3、表单布局 TableLayout 
           和TableRow配合使用,和HTML里的Table相似。 

    举个例子: 

     1 <?xml version="1.0" encoding="utf-8" ?>
     2 
     3 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"android:layout_height="fill_parent" android:stretchColumns="1">
     4 <TableRow>
     5 
     6 <TextView 
     7 android:layout_column="1" 
     8 android:text="打开..."
     9 android:padding="3dip" />
    10 
    11 <TextView 
    12 android:text="Ctrl-O" 
    13 android:gravity="right" 
    14 android:padding="3dip" />
    15 </TableRow>
    16 <TableRow>
    17 
    18 
    19 <TextView 
    20 android:layout_column="1" 
    21 android:text="保存..." 
    22 android:padding="3dip" />
    23 
    24 <TextView 
    25 android:text="Ctrl-S" 
    26 android:gravity="right" 
    27 android:padding="3dip" />
    28 </TableRow>
    29 
    30 <TableRow>
    31 <TextView 
    32 android:layout_column="1" 
    33 android:text="另存为..." 
    34 android:padding="3dip" />
    35 
    36 <TextView 
    37 android:text="Ctrl-Shift-S" 
    38 android:gravity="right" 
    39 android:padding="3dip" />
    40 </TableRow>
    41 
    42 <View 
    43 android:layout_height="2dip" 
    44 android:background="#FF909090" />
    45 <TableRow>
    46 <TextView android:text="*" android:padding="3dip" />
    47 <TextView android:text="导入..." android:padding="3dip" />
    48 </TableRow>
    49 <TableRow>
    50 <TextView android:text="*" android:padding="3dip" />
    51 <TextView android:text="导出..." android:padding="3dip" />
    52 <TextView android:text="Ctrl-E" android:gravity="right" android:padding="3dip" />
    53 </TableRow>
    54 
    55 <View android:layout_height="2dip" android:background="#FF909090" />
    56 <TableRow>
    57 <TextView android:layout_column="1" android:text="退出" android:padding="3dip" />
    58 
    59 </TableRow>
    60 
    61 </TableLayout>

    4、切换卡 Tabwidget 
           继承TabActivity,实现标签的切换功能。 
    举个例子: 

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
     3 android:id="@android:id/tabhost"
     4 android:layout_width="fill_parent"
     5 android:layout_height="fill_parent">
     6 <LinearLayout
     7 android:orientation="vertical"
     8 android:layout_width="fill_parent"
     9 android:layout_height="fill_parent">
    10 <TabWidget
    11 android:id="@android:id/tabs"
    12 android:layout_width="fill_parent"
    13 android:layout_height="wrap_content" />
    14 <FrameLayout
    15 android:id="@android:id/tabcontent"
    16 android:layout_width="fill_parent"
    17 android:layout_height="fill_parent">
    18 <TextView 
    19 android:id="@+id/textview1"
    20 android:layout_width="fill_parent"
    21 android:layout_height="fill_parent" 
    22 android:text="this is a tab" />
    23 <TextView 
    24 android:id="@+id/textview2"
    25 android:layout_width="fill_parent"
    26 android:layout_height="fill_parent" 
    27 android:text="this is another tab" />
    28 <TextView 
    29 android:id="@+id/textview3"
    30 android:layout_width="fill_parent"
    31 android:layout_height="fill_parent" 
    32 android:text="this is a third tab" />
    33 </FrameLayout>
    34 </LinearLayout>
    35 </TabHost>

    其他布局:

           1、帧布局 FrameLayout:

           是最简单的一个布局对象。在他里面的的所有显示对象爱你过都将固定在屏幕的左上角,不能指定位置,但允许有多个显示对象,只是后一个会直接覆盖在前一个之上显示,会把前面的组件部分或全部挡住。

    举个例子:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <FrameLayout
     3 xmlns:android="http://schemas.android.com/apk/res/android"
     4 android:layout_width="fill_parent"
     5 android:layout_height="fill_parent">
     6 <TextView
     7 android:text="big"
     8 android:layout_width="wrap_content"
     9 android:layout_height="wrap_content"
    10 android:textSize="50pt"/>
    11 <TextView
    12 android:text="middle"
    13 android:layout_width="wrap_content"
    14 android:layout_height="wrap_content"
    15 android:textSize="20pt"/>
    16 <TextView
    17 android:text="small"
    18 android:layout_width="wrap_content"
    19 android:layout_height="wrap_content"
    20 android:textSize="10pt"/>
    21 </FrameLayout>

    2、绝对布局 AbsoluteLayout

           绝对定位AbsoluteLayout,又可以叫做坐标布局,可以直接指定子元素的绝对位置,这种布局简单直接,直观性强,但是由于手机屏幕尺寸差别比较大,使用绝对定位的适应性会比较差。分辨率不一样的屏幕,显示的位置也会有所不同。

    举个例子:

     1 < ?xml version="1.0" encoding="utf-8"?>
     2 < AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3 android:orientation="vertical"
     4 android:layout_width="fill_parent"
     5 android:layout_height="fill_parent"
     6 >
     7 
     8 < EditText
     9 android:text="Welcome to Mr Wei's blog"
    10 android:layout_width="fill_parent"
    11 android:layout_height="wrap_content"/>
    12 
    13 < Button
    14 android:layout_x="250px" //设置按钮的X坐标
    15 android:layout_y="40px" //设置按钮的Y坐标
    16 android:layout_width="70px" //设置按钮的宽度
    17 android:layout_height="wrap_content"
    18 android:text="Button"/>
    19 
    20 < /AbsoluteLayout>
  • 相关阅读:
    循环队列操作
    让测试人员参与软件设计
    Oracle之初探
    关注LoadRunner脚本回放日志中的Warning信息
    性能测试工具CurlLoader
    『原创』网站测试计划模板
    LoadRunner如何监控Linux下的系统资源
    搭建Linux学习环境安装CentOS5.4
    Linux下搭建Tomcat服务器
    性能测试分析之带宽瓶颈的疑惑
  • 原文地址:https://www.cnblogs.com/smart9595/p/3721089.html
Copyright © 2011-2022 走看看