`
javaxhw
  • 浏览: 36239 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

学习android之布局

阅读更多
原文地址:http://blog.sina.com.cn/s/blog_694448320100l3dg.html
andoid的UI组件学习,首先需要需要学习组件的布局,各种组件均必须放到布局里面,才可以显示,下面就记录下几种常用的布局:
1、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="fill_parent">
</LinearLayout>
属性介绍:
android:orientation="vertical"  表示这个布局里面的组件子元素都是上下垂直的摆放
android:orientation="horizontal"  表示这个布局里面的组件子元素都是左右水平的摆放
android:layout_width="fill_parent"  表示布局的面板的宽度占满屏幕
android:layout_height="fill_parent"  表示布局的面板的高度占满屏幕
2、RelativeLayout(相对布局)
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="@drawable/blue" android:padding="10dip">
<TextView android:id="@+id/label" android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:text="请输入用户名:" />
<!--
  这个EditText放置在上边id为label的TextView的下边
-->
<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" />
<!--
  取消按钮和容器的右边齐平,并且设置左边的边距为10dip
-->
<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>
属性介绍:
android:layout_below="@id/label"       表示将该元素至于id为label的的组件的下面
android:layout_marginLeft="10dip"      表示改元素里左边的边距为10PX;
android:layout_alignTop="@id/cancel"   表示将顶部与id为cancel的组件的顶部对齐
android:layout_alignParentRight="true"  表示该组件永远都放置于右边
android:layout_toLeftOf="@id/cancel"    表示该组件位于id为cancel的左边
TableLayout(表格布局)
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:stretchColumns="1" android:strechColumns="0,1,2,3">
<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>
属性说明:
<TableLayou>是顶级元素,说明采用的是表格布局
<TableRow>定义一个行
<TextView>定义一个单元格的内容
android:gravity="right"     表示文字对齐方式
android:padding="3dip"      表示视图与视图内容之间的间隙距离
android:strechColumns="0,1,2,3"  表示每行都由"0123"列占满空白空间
暂时先写这么多,接下来我会接着增加的
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics