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. android 学习随笔十三(网络:多线程下载)

    多线程断点续传下载1.多线程:快* 原理:抢占服务器资源* 单线程下载:线程从第0个字节开始下,下到最后一个字节,在本地硬盘的临时文件中从第0个字节开始写,写到最后一个字节,下载完成时,临时文件也写完 ...

  2. sql字段类型介绍

    1 表格与储存引擎 表格(table)是数据库中用来储存纪录的基本单位,在建立一个新的数据库以后,你必须为这个数据库建立一些储存资料的表格: 每一个数据库都会使用一个资料夹,这些数据库资料夹用来储存所 ...

  3. zjtd 2016面试

    1.写一个函数get_next() class A{ public :int next();   //取下一个值,并且指针后移 bool has_next(); private: //可以认为是一个q ...

  4. js实现鼠标的滑动

    js实现鼠标的滑动 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...

  5. 《深度探索C++对象模型》3

    第六章:执行期语意学 全局对象的初始化: 局部静态对象的初始化: 构建对象数组的vec_new()函数: 针对数组的new操作: placement operator new: 反聚合提高效率: 模板 ...

  6. RadioGroup+Fragment 使用Fragment的add()方法,防止使用replace每次都重新加载页面,造成资源浪费

    radiogroup+fragment是很常用的主页导航控件,之前为了代码简便一直使用的replace()替换fragment,代码如下: getSupportFragmentManager().be ...

  7. python: html 笔记2

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 八大排序算法之六--交换排序—快速排序(Quick Sort)

    基本思想: 1)选择一个基准元素,通常选择第一个元素或者最后一个元素, 2)通过一趟排序讲待排序的记录分割成独立的两部分,其中一部分记录的元素值均比基准元素值小.另一部分记录的 元素值比基准值大. 3 ...

  9. 详解KMP算法

    转载注明出处:http://www.cnblogs.com/yjiyjige/p/3263858.html 什么是KMP算法: KMP是三位大牛:D.E.Knuth.J.H.Morris和V.R.Pr ...

  10. jqeury之轮播图

    $(document).ready(function(){ var sWidth = $('#pic1').width(); var len = $('#pic1 .sildebar li').len ...