RelativeLayout与LinearLayout的比较

来源:互联网 发布:淘宝卖衣服从哪进货 编辑:程序博客网 时间:2024/05/23 13:31

转载自: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="http://schemas.android.com/apk/res/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="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/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 的实现思路还是挺清晰的,所以部署起来还是不算很难。
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(17) | 评论(0) | 转发(0) |
0

上一篇:Qt标准对话框之QMessageBox

下一篇:android studio logcat异常调试信息解析错误方法

相关热门文章
  • 开源负载均衡LVS随机自启动异...
  • 摄像头avcodec_encode_video2...
  • 欢迎比较小的大提琴在ChinaUni...
  • BLE-NRF51822教程8-动态广播...
  • 音视频聊天开发: 9 声音 建议...
  • Android之开发环境搭建
  • Android自定义View的实现...
  • AndroidManifest.xml配置文件...
  • Android相对布局+圆角按钮+Sha...
  • 查看Android应用包名package和...
  • linux dhcp peizhi roc
  • 关于Unix文件的软链接
  • 求教这个命令什么意思,我是新...
  • sed -e "/grep/d" 是什么意思...
  • 谁能够帮我解决LINUX 2.6 10...
给主人留下些什么吧!~~
原创粉丝点击