Chinaunix首页 | 论坛 | 博客
  • 博客访问: 421894
  • 博文数量: 113
  • 博客积分: 2228
  • 博客等级: 大尉
  • 技术积分: 1341
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-24 15:07
文章分类

全部博文(113)

文章存档

2013年(52)

2012年(61)

我的朋友

分类: 嵌入式

2013-01-09 10:54:53

Android入门学习_代码常用布局 

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

android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 

 

android:id="@+id/firstText" 
android:text="第一行一行一行一行一行一行一行一行一行一行" 
android:gravity="center_vertical" 
android:textSize="35pt" 
android:background="#aa0000" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:paddingLeft="10dip" 
android:paddingTop="20dip" 
android:paddingRight="30dip" 
android:paddingBottom="40dip" 
android:layout_weight="1" 
android:singleLine="true"/> 

android:id="@+id/secondText" 
android:text="第二行" 
android:gravity="center_vertical" 
android:textSize="15pt" 
android:background="#0000aa" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_weight="1"/> 
  
2、相对布局 RelativeLayout  
相对布局 RelativeLayout 允许子元素指定它们相对于其父元素或兄弟元素的位置,这是实际布局 
中最常用的布局方式之一。它灵活性大很多,当然属性也多,操作难度也大,属性之间产生冲突 
的的可能性也大,使用相对布局时要多做些测试。 
举个例子: 
java代码:  
 
 

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

android:id="@+id/label" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="Type here:" /> 

android:id="@+id/entry" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:background=" :drawable/editbox_background" 
android:layout_below="@id/label" /> 

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" /> 

android:layout_height="wrap_content" 
android:layout_toLeftOf="@id/ok" 
android:layout_alignTop="@id/ok" 
android:text="Cancel" /> 
  
3、表单布局 TableLayout  
和TableRow配合使用,和HTML里的Table相似。 
举个例子: 
java代码: 
 

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

android:layout_column="1" 
android:text="打开..." 
android:padding="3dip" /> 

android:text="Ctrl-O" 
android:gravity="right" 
android:padding="3dip" /> 
 
 

android:layout_column="1" 
android:text="保存..." 
android:padding="3dip" /> 

android:text="Ctrl-S" 
android:gravity="right" 
android:padding="3dip" /> 
 
 

android:layout_column="1" 
android:text="另存为..." 
android:padding="3dip" /> 

android:text="Ctrl-Shift-S" 
android:gravity="right" 
android:padding="3dip" /> 
 

android:layout_height="2dip" 
android:background="#FF909090" /> 
 
 
 
 
 
 
 
 
 
 
 
 
 
  
4、切换卡 Tabwidget  
继承TabActivity,实现标签的切换功能。  
举个例子: 
java代码:  
 

android:id=" :id/tabhost" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

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

android:id=" :id/tabs" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" /> 

android:id=" :id/tabcontent" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

android:id="@+id/textview1" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:text="this is a tab" /> 

android:id="@+id/textview2" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:text="this is another tab" /> 

android:id="@+id/textview3" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:text="this is a third tab" /> 
 
 
  
其他布局: 
1、帧布局 FrameLayout: 
是最简单的一个布局对象。在他里面的的所有显示对象爱你过都将固定在屏幕的左上角,不能指 
定位置,但允许有多个显示对象,只是后一个会直接覆盖在前一个之上显示,会把前面的组件部 
分或全部挡住。 
举个例子: 
java代码: 
 

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

android:text="big" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:textSize="50pt"/> 

android:text="middle" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:textSize="20pt"/> 

android:text="small" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:textSize="10pt"/> 
  
2、绝对布局 AbsoluteLayout 
绝对定位AbsoluteLayout,又可以叫做坐标布局,可以直接指定子元素的绝对位置,这种布局简 
单直接,直观性强,但是由于手机屏幕尺寸差别比较大,使用绝对定位的适应性会比较差。分辨 
率不一样的屏幕,显示的位置也会有所不同。 
举个例子: 
java代码: 
< ?xml version="1.0" encoding="utf-8"?> 
< AbsoluteLayout xmlns:android=" " 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 

< EditText 
android:text="Welcome to Mr Wei's 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> 


阅读(912) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~