Chinaunix首页 | 论坛 | 博客
  • 博客访问: 283795
  • 博文数量: 82
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 874
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-21 09:58
个人简介

traveling in cumputer science!!

文章分类

全部博文(82)

文章存档

2016年(13)

2015年(69)

我的朋友

分类: Android平台

2015-06-16 13:08:27

转载自:http://blog.csdn.net/onepiece2/article/details/26396287

RelativeLayout

是相对布局在页面上相对于页面坐标进行布局设置。比如可以通过确定对象A确定对象B的位置,B可以在A的上下左右,对象B距离A的位置。

RelativeLayout的灵活性很高,但在实际操作过程中我很难确定定位对象的位置,最后用图形界面手托才完成页面的布局。


实现代码如下


点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android=""
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent" >

  5.     <TextView
  6.         android:id="@+id/userName"
  7.         android:layout_width="wrap_content"
  8.         android:layout_height="wrap_content"
  9.         android:layout_alignBottom="@+id/tipUserName"
  10.         android:layout_alignParentTop="true"
  11.         android:text="@string/user_name"
  12.         android:textSize="20sp" />

  13.     <EditText
  14.         android:id="@+id/tipUserName"
  15.         android:layout_width="match_parent"
  16.         android:layout_height="wrap_content"
  17.         android:layout_toRightOf="@id/userName"
  18.         android:inputType="text"
  19.         android:text="@string/tip_user_name" />

  20.     <TextView
  21.         android:id="@+id/passWord"
  22.         android:layout_width="wrap_content"
  23.         android:layout_height="wrap_content"
  24.         android:layout_above="@+id/checkBox"
  25.         android:layout_alignParentLeft="true"
  26.         android:layout_below="@id/userName"
  27.         android:layout_toLeftOf="@+id/tipUserName"
  28.         android:text="@string/user_password"
  29.         android:textSize="20sp" />

  30.     <EditText
  31.         android:id="@+id/tipPassword"
  32.         android:layout_width="match_parent"
  33.         android:layout_height="wrap_content"
  34.         android:layout_below="@id/tipUserName"
  35.         android:layout_toRightOf="@id/passWord"
  36.         android:inputType="textPassword"
  37.         android:text="@string/tip_user_password" />

  38.     <Button
  39.         android:id="@+id/logInBtn"
  40.         android:layout_width="wrap_content"
  41.         android:layout_height="wrap_content"
  42.         android:layout_below="@id/passWord"
  43.         android:text="@string/login_Btn" />

  44.     <CheckBox
  45.         android:id="@+id/checkBox"
  46.         android:layout_width="match_parent"
  47.         android:layout_height="wrap_content"
  48.         android:layout_below="@id/tipPassword"
  49.         android:layout_toRightOf="@id/logInBtn"
  50.         android:text="@string/rember_pass" />

  51. </RelativeLayout>


用户名和密码字体过小,可用android:textSize="XXsp"调整字体大小。

我本来想通过用户名框的位置定下用户名输入框的位置和密码框的位置,再通过密码框的位置定位到密码输入框的和登陆按钮的位置,最后通过登陆按钮来确定单选框的位置。最后结果就是由于用户名框和密码框太小,导致部分挤在了一起,特别难看。

LinearLayout 

线性布局也是一种比较灵活的布局方式, 通过直接的线性布局对页面直接实现布局。

在使用LinearLayout 的时候,思路大致是将页面母板分成若干部分,然后母板LinearLayout 使用android:orientation="vertical"将各个部分垂直分布,然后每个部分中的各个对象通过android:orientation="horizontal"实现各个对象的横向分布。

实现代码如下


点击(此处)折叠或打开

  1. <LinearLayout xmlns:android=""
  2.     xmlns:tools=""
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical"
  6.     tools:context="${packageName}.${activityClass}"
  7.     tools:ignore="Orientation" >

  8.     <LinearLayout
  9.         android:layout_width="match_parent"
  10.         android:layout_height="wrap_content"
  11.         android:orientation="horizontal" >

  12.         <TextView
  13.             android:id="@+id/userName"
  14.             android:layout_width="wrap_content"
  15.             android:layout_height="wrap_content"
  16.             android:text="@string/user_name" />

  17.         <EditText
  18.             android:id="@+id/tipUserName"
  19.             android:layout_width="0dp"
  20.             android:layout_height="wrap_content"
  21.             android:layout_weight="1"
  22.             android:inputType="text"
  23.             android:text="@string/tip_user_name" />
  24.     </LinearLayout>

  25.     <LinearLayout
  26.         android:layout_width="match_parent"
  27.         android:layout_height="wrap_content"
  28.         android:orientation="horizontal" >

  29.         <TextView
  30.             android:id="@+id/passWord"
  31.             android:layout_width="wrap_content"
  32.             android:layout_height="wrap_content"
  33.             android:text="@string/user_password" />

  34.         <EditText
  35.             android:id="@+id/tipPassword"
  36.             android:layout_width="0dp"
  37.             android:layout_height="wrap_content"
  38.             android:layout_weight="1"
  39.             android:inputType="textPassword"
  40.             android:text="@string/tip_user_password" />
  41.     </LinearLayout>

  42.     <LinearLayout
  43.         android:layout_width="match_parent"
  44.         android:layout_height="wrap_content"
  45.         android:orientation="horizontal" >

  46.         <Button
  47.             android:id="@+id/logInBtn"
  48.             android:layout_width="wrap_content"
  49.             android:layout_height="wrap_content"
  50.             android:text="@string/login_Btn" />

  51.         <CheckBox
  52.             android:id="@+id/checkBox"
  53.             android:layout_width="wrap_content"
  54.             android:layout_height="wrap_content"
  55.             android:text="@string/rember_pass" />
  56.     </LinearLayout>

  57. </LinearLayout>



在使用LinearLayout 的时候要注意将各个对象包裹进 LinearLayout 。已经有了母板LinearLayout 就不用再重行创建LinearLayout 面板了。对于LinearLayout 的实现思路还是挺清晰的,所以部署起来还是不算很难。
阅读(1714) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~