LinearLayout在Android中被广泛使用,LinearLayout有一个比较重要的属性——android:layout_weight。按照字面理解就是该控件的权重,这个值默认是 零(0)。

  举个例子:

  在下图中,整个界面是一个Linearlayout,包含了5个子控件Linerarlayout,分别是空的、用户名的、密码的、确认和取消按钮的、保存用户的layout。

  在这里,先给整个界面设置20个权重,再给子控件layout分别设置:10、1、2、3、1个权重,最后的效果就是上图。


  Xml文件代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mailLayout"
android:orientation="vertical"
android:background="@drawable/login_background"
android:weightSum="20"
>
<!-- 空白部分 -->
<LinearLayout
android:id="@+id/loginSpace"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="10"
> </LinearLayout>
<!-- 登陆用户名 -->
<LinearLayout
android:id="@+id/loginNameLayout"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/loginTextName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/login_text_name"
/>
<EditText
android:id="@+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:layout_marginLeft="10dip"
android:background="@drawable/login_name_input"
/>
</LinearLayout>
<!-- 登陆密码 -->
<LinearLayout
android:id="@+id/loginPasswordLayout"
android:layout_weight="2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/loginTextPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/login_text_pwd"
/>
<EditText
android:id="@+id/passWord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:password="true"
android:layout_marginLeft="11dip"
android:background="@drawable/login_pwd_input"
/>
</LinearLayout> <!-- 确认登陆按钮和取消按钮 -->
<LinearLayout
android:id="@+id/loginButtonLayout"
android:layout_weight="3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
> <ImageButton
android:id="@+id/loginButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/login_confirm_button"
android:background="@null"
/>
<ImageButton
android:id="@+id/cancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dip"
android:src="@drawable/login_text_cancel"
android:background="@null" />
</LinearLayout> <!-- 保存用户名和密码 -->
<LinearLayout
android:id="@+id/loginSaveUserLayout"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal"
>
<CheckBox
android:id="@+id/saveUserInfoCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/login_checkbox"
android:checked="true" />
<TextView
android:id="@+id/loginTextSaveUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/login_saveuser" />
</LinearLayout>
</LinearLayout>

Android——学习:线性布局权重分配的更多相关文章

  1. Android学习----五大布局

    1.LinearLayout 线性布局 android:orientation="horizontal" 制定线性布局的排列方式 水平 horizontal 垂直 vertical ...

  2. Android LinearLayout线性布局

    LinearLayout是线性布局控件:要么横向排布,要么竖向排布 决定性属性:必须有的! android:orientation:vertical (垂直方向) .horizontal(水平方向) ...

  3. android 学习Layout布局的使用

    android 常用布局 LinearLayout(线性布局) 线性的 垂直的 水平的RelativeLaytout(相对布局) 最灵活的TableLayout(表格布局) 使用GridView代替A ...

  4. android 学习 之 布局(上)

    学习安卓布局前,先了解三个属性值: 1.fill_parent: 设置一个构件的布局为fill_parent将强制性地使构件扩展,以填充布局单元内尽可能多的空间 2.match_parent: And ...

  5. Android LinearLayout线性布局详解

    为了更好地管理Android应用的用户界面里的各组件,Android提供了布局管理器.通过使用布局管理器,Android应用图形用户界面具有良好的平台无关性.推荐使用布局管理器来管理组件的分布.大小, ...

  6. Android学习5—布局简介

    Android界面的布局主要有四种,分别为RelativeLayout.LinearLayout.TableLayout.FrameLayout,接下来分别介绍这些布局如何使用(为了简单起见,接下来的 ...

  7. Android学习——LinearLayout布局实现居中、左对齐、右对齐

    android:orientation="vertical"表示该布局下的元素垂直排列: 在整体垂直排列的基础上想要实现内部水平排列,则在整体LinearLayout布局下再创建一 ...

  8. Android 使用线性布局LinearLayout和Button实现一个点红块游戏

    这个游戏的功能类似打地鼠. 项目地址:https://github.com/moonlightpoet/RedBlock 程序下载试玩地址:https://github.com/moonlightpo ...

  9. android 学习 之 布局(下)LinearLayout,RelativeLayout,TableLayout,FrameLayout

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...

随机推荐

  1. button改变背景与文字颜色

    1.定义/zhsh/res/color/txt_guide_selector.xml <?xml version="1.0" encoding="utf-8&quo ...

  2. maven相关

    1. 创建/导入maven项目时 eclipse默认jdk版本配置:http://blog.csdn.net/lzj0470/article/details/42292021 2. eclipse 使 ...

  3. python随机数的使用记录

    一.random模块简介 Python标准库中的random函数,可以生成随机浮点数.整数.字符串,甚至帮助你随机选择列表序列中的一个元素,打乱一组数据等. 二.random模块重要函数 1 ).ra ...

  4. 对EJB返回的AaaryList显示到table的处理方法

      1. ArrayList --> Object[]        ArrayList x = new ArrayList();        int i = x.size();        ...

  5. ubunt1204安装配置vsftp

    本文将搭建一个最简单的ftp服务,即通过root用户可进行登录.上传.下载,具体步骤如下: 1.安装vsftpd服务 sudo apt-get install vsftpd 2.编辑vsftp配置文件 ...

  6. 阅读推荐——深入浅出Mesos

    深入浅出Mesos(一):为软件定义数据中心而生的操作系统http://www.infoq.com/cn/articles/analyse-mesos-part-01 深入浅出Mesos(二):Mes ...

  7. PHP 设计模式之观察者模式

    现在有两派,有的人建议使用设计模式,有的人不建议使用设计模式!这就向写文章一样,有的人喜欢文章按照套路走,比如叙事性质的文章,时间,地点,人物,事件.而有的人喜欢写杂文或者散文,有的人喜欢写诗词! 现 ...

  8. java面试每日一题8

    题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身.例如:153是一个"水仙花数",因为153=1 ...

  9. LeetCode----67. Add Binary(java)

    package addBinary67;/* Given two binary strings, return their sum (also a binary string).For example ...

  10. 判断sql执行效率以及针对临时表的使用

    /****** Script for SelectTopNRows command from SSMS ******/ SET STATISTICS io ON SET STATISTICS time ...