android中的常用布局

android 常用布局
1、线性布局 LinearLayout:
线性布局是所有布局中最常用的类之一,也是RadioGroup, TabWidget, TableLayout, TableRow, ZoomControls类的父类。LinearLayout可以让它的子元素垂直或水平的方式排成一行(不设置方向的时候默认按照垂直方向排列)。
举个例子:
<?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="wrap_content"
    >
    <!--
     android:id  —— 为控件指定相应的ID
     android:text —— 指定控件当中显示的文字,需要注意的是,这里尽量使用strings.xml文件当中的字符串
     android:grivity —— 指定控件的基本位置,比如说居中,居右等位置
     android:textSize —— 指定控件当中字体的大小
     android:background —— 指定该控件所使用的背景色,RGB命名法
     android:width —— 指定控件的宽度
     android:height —— 指定控件的高度
     android:padding* —— 指定控件的内边距,也就是说控件当中的内容
     android:layout_weight —— 控件之间的权重比
     android:sigleLine —— 如果设置为真的话,则将控件的内容在同一行当中进行显示
     -->
 <TextView
  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"/>
 <TextView
  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"/>
</LinearLayout>
2、相对布局 RelativeLayout
相对布局 RelativeLayout 允许子元素指定它们相对于其父元素或兄弟元素的位置,这是实际布局中最常用的布局方式之一。它灵活性大很多,当然属性也多,操作难度也大,属性之间产生冲突的的可能性也大,使用相对布局时要多做些测试。
举个例子:
<?xml version="1.0" encoding="utf-8"?>
 <!--
  android:layout_above 将该控件的底部至于给定ID的控件之上
  android:layout_below 将该控件的顶部至于给定ID的控件之下
  android:layout_toLeftOf 将该控件的右边缘和给定ID的控件的左边缘对齐
  android:layout_toRightOf 将该控件的左边缘和给定ID的控件的右边缘对齐
  android:layout_alignBaseline 该控件的baseline和给定ID的控件的baseline对齐
  android:layout_alignBottom 将该控件的底部边缘与给定ID控件的底部边缘
  android:layout_alignLeft 将该控件的左边缘与给定ID控件的左边缘对齐
  android:layout_alignRight 将该控件的右边缘与给定ID控件的右边缘对齐
  android:layout_alignTop 将给定控件的顶部边缘与给定ID控件的顶部对齐

  android:alignParentBottom 如果该值为true,则将该控件的底部和父控件的底部对齐
  android:layout_alignParentLeft 如果该值为true,则将该控件的左边与父控件的左边对齐
  android:layout_alignParentRight 如果该值为true,则将该控件的右边与父控件的右边对齐
  android:layout_alignParentTop 如果该值为true,则将空间的顶部与父控件的顶部对齐
  android:layout_centerHorizontal 如果值为真,该控件将被至于水平方向的中央
  android:layout_centerInParent 如果值为真,该控件将被至于父控件水平方向和垂直方向的中央
  android:layout_centerVertical 如果值为真,该控件将被至于垂直方向的中央
 -->
<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="Type here:" />
 <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/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>
3、表单布局 TableLayout
和TableRow配合使用,和HTML里的Table相似。
举个例子:
<?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="打开..." android:padding="3dip" />
  <TextView android:text="Ctrl-O" android:gravity="right" android:padding="3dip" />
  </TableRow>
- <TableRow>
  <TextView android:layout_column="1" android:text="保存..." android:padding="3dip" />
  <TextView android:text="Ctrl-S" android:gravity="right" android:padding="3dip" />
  </TableRow>
- <TableRow>
  <TextView android:layout_column="1" android:text="另存为..." android:padding="3dip" />
  <TextView android:text="Ctrl-Shift-S" android:gravity="right" android:padding="3dip" />
  </TableRow>
  <View android:layout_height="2dip" android:background="#FF909090" />
- <TableRow>
  <TextView android:text="*" android:padding="3dip" />
  <TextView android:text="导入..." android:padding="3dip" />
  </TableRow>
- <TableRow>
  <TextView android:text="*" android:padding="3dip" />
  <TextView android:text="导出..." android:padding="3dip" />
  <TextView android:text="Ctrl-E" android:gravity="right" android:padding="3dip" />
  </TableRow>
  <View android:layout_height="2dip" android:background="#FF909090" />
- <TableRow>
  <TextView android:layout_column="1" android:text="退出" android:padding="3dip" />
  </TableRow>
  </TableLayout>
4、切换卡 Tabwidget
继承TabActivity,实现标签的切换功能。
举个例子:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <TextView
                android:id="@+id/textview1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="this is a tab" />
            <TextView
                android:id="@+id/textview2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="this is another tab" />
            <TextView
                android:id="@+id/textview3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="this is a third tab" />
     </FrameLayout>
    </LinearLayout>
</TabHost>
其他布局:
1、帧布局 FrameLayout:
是最简单的一个布局对象。在他里面的的所有显示对象爱你过都将固定在屏幕的左上角,不能指定位置,但允许有多个显示对象,只是后一个会直接覆盖在前一个之上显示,会把前面的组件部分或全部挡住。
举个例子:
<?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">
    <TextView
        android:text="big"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="50pt"/>
    <TextView
        android:text="middle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20pt"/>
    <TextView
        android:text="small"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10pt"/>
</FrameLayout>
2、绝对布局 AbsoluteLayout
绝对定位AbsoluteLayout,又可以叫做坐标布局,可以直接指定子元素的绝对位置,这种布局简单直接,直观性强,但是由于手机屏幕尺寸差别比较大,使用绝对定位的适应性会比较差。分辨率不一样的屏幕,显示的位置也会有所不同。
举个例子:
< ?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 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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值