Chinaunix首页 | 论坛 | 博客
  • 博客访问: 298934
  • 博文数量: 53
  • 博客积分: 1266
  • 博客等级: 少尉
  • 技术积分: 572
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-16 16:45
文章分类

全部博文(53)

文章存档

2012年(37)

2011年(16)

分类: 嵌入式

2011-10-16 15:27:04

一、常用布局方式

1FrameLayout    框架布局,依次在四方矩形左上角堆放,后放的在上面,先放的在下面

2LinearLayout    线性布局,分为垂直和水平依次排队放置

3AbsoluteLayout  绝对布局,需要指定左上角的xy

4RelativeLayout   相对布局,相对左右上下UI布局

5TableLayout     表格布局,类似表格

 

二、FrameLayout框架布局

main.xml

<FrameLayout xmlns:android=""

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

    <Button android:text="Button1" android:layout_width="262dp" android:id="@+id/button1" android:layout_height="242dp">Button>

    <Button android:text="Button2" android:id="@+id/button2" android:layout_width="142dp" android:layout_height="98dp">Button>

FrameLayout>

 

三、LinearLayout线性布局

main.xml

<LinearLayout xmlns:android=""

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="horizontal">

    <Button android:text="Button2" android:layout_width="142dp" android:id="@+id/button2" android:layout_height="98dp">Button>

    <Button android:text="Button1" android:id="@+id/button1" android:layout_width="164dp" android:layout_height="163dp">Button>

LinearLayout>

 

四、AbsoluteLayout 绝对布局

main.xml

<AbsoluteLayout xmlns:android=""

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="horizontal">

    <Button android:text="Button1" android:layout_width="164dp" android:layout_height="163dp" android:id="@+id/button1" android:layout_x="52dp" android:layout_y="126dp">Button>

    <Button android:text="Button2" android:layout_width="142dp" android:layout_height="98dp" android:id="@+id/button2" android:layout_x="89dp" android:layout_y="6dp">Button>

AbsoluteLayout>

 

五、RelativeLayout 相对布局

main.xml

<RelativeLayout xmlns:android=""

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

    android:padding="10dip">

 

    <TextView android:id="@+id/label" android:layout_width="fill_parent"

       android:layout_height="wrap_content" android:text="请输入用户名:" />

 

   

    <EditText android:id="@+id/entry" android:layout_width="fill_parent"

       android:layout_height="wrap_content" android:background="@android:drawable/editbox_background"

       android:layout_below="@id/label" />

 

   

    <Button android:id="@+id/cancel" 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="取消" />

 

   

    <Button android:id="@+id/ok" android:layout_width="wrap_content"

       android:layout_height="wrap_content" android:layout_toLeftOf="@id/cancel"

       android:layout_alignTop="@id/cancel" android:text="确定" />

 

RelativeLayout>

 

六、TableLayout 表格布局

main.xml

<TableLayout xmlns:android=""

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

    android:stretchColumns="1">

    <TableRow>

       <TextView android:text="用户名:" android:textStyle="bold"

           android:gravity="right" android:padding="3dip" />

       <EditText android:id="@+id/username" android:padding="3dip"

           android:scrollHorizontally="true" />

    TableRow>

    <TableRow>

       <TextView android:text="登录密码:" android:textStyle="bold"

           android:gravity="right" android:padding="3dip" />

       <EditText android:id="@+id/password" android:password="true"

           android:padding="3dip" android:scrollHorizontally="true" />

    TableRow>

    <TableRow android:gravity="right">

       <Button android:id="@+id/cancel" android:text="取消" />

       <Button android:id="@+id/login" android:text="登录" />

    TableRow>

TableLayout>

 

七、动态设置布局

LinearLayout layoutMain = new LinearLayout(this);

layoutMain.setOrientation(LinearLayout.HORIZONTAL);       

setContentView(layoutMain);

 

八、在布局中加入布局

layoutMain.addView(layoutLeft,100,100);

layoutMain.addView(layoutRight,relParam);

上述代码是在layoutMain布局中加入layoutLeftlayoutRight两个布局

1layoutMain.addView(layoutLeft,100,100);中的100100是加入布局的高度和宽度

2layoutMain.addView(layoutRight,relParam);中的relParam是布局参数对象,

RelativeLayout.LayoutParams relParam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);

 

九、获取xml资源文件对应布局

LayoutInflater inflate = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

RelativeLayout layoutLeft = (RelativeLayout)inflate.inflate(R.layout.left, null);

 

十、其他相关设置

android:background="@drawable/blue" //  设置背景

android:padding="10dip"          // 边距

android:layout_alignParentRight="true" // 与父控件对齐方式

android:stretchColumns="1"             // 作用是让第2列可以扩展到所有可用空间

android:gravity="right"                // 对齐方式

android:scrollHorizontally="true"      // 水平滚动

 

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