LinearLayout线性布局搭配权重属性的使用
在开发中,我们是通过布局来完成应用界面的搭配的,通过各种布局,我们可以完成各种复杂的界面设计。而LinearLayout也就是我们说的线性布局,这个比较简单而且使用很广泛的一种布局。下面我们通过一个Demo来对这个布局进行学习。我们先来看看效果图吧。

然后在来看看布局文件main_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.linerlayoutdemo.MainActivity" > <!-- 这个例子很简单主要知识点也就两个 -->
<!-- 1、布局嵌套,这个例子通过在一个线性布局里面嵌套了两个垂直方向线性布局 -->
<!-- 2、利用android:layout_weight这个属性平均分配宽度和高度,也就是权重属性 --> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" > <TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#120ff0" /> <TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#950ff0" /> <TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@android:color/darker_gray" /> <TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#000ff0" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal" > <TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#079ff0" /> <TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000fca" /> <TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000f65" /> <TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/holo_blue_bright" />
</LinearLayout> </LinearLayout>
这里我们首先说一下LinearLayout,线性布局有两个方向,水平和垂直方向。分别是通过android:orientation="horizontal"和android:orientation="vertical"来控制的。这里说一个记忆的小技巧。因为小编英文不太好。经常忘记那个是水平那个是垂直。所以我就记住horizontal是H开头的。这个是水平方向,那么水平就是横向嘛!hen,H开头。这样就不会记错了。
从这个Demo的效果图我们可以很清楚的看出,Demo在一个垂直方向的线性布局里面分别嵌套了两个线性布局。上面一个是horizontal方向,下面一个是vertical方向。
说完布局我们顺便说一说这个Demo的知识点。代码注释我们有说,这个Demo主要两个知识点,一个是布局嵌套,一个是权重分配。这里们说一说权重。
权重,也就是对控件设置 android:layout_weight的属性。这个属性的意思是分配剩余空间。注意,是剩余空间,所以,我们一般将控件的宽度或者高度设置为0dp(至于是宽度还是高度就是看布局是水平还是垂直了)。这里我先说简单的。
比如有俩个控件,分别设置为android:layout_weight=“1”,android:layout_weight=“2”,表示控件分别占屏幕的1/3和2/3,。不过这是有一个前提的,就是建立在控件的宽度或者高度设置为0dp的情况下。
然后我们说一下复杂的。其实也不复杂。权重是分配剩余空间的意思就是说,权重属性他是对屏幕剩下的空间进行分配。这就是为什么上面我们说要将控件的宽度或者高度设置为0dp的原因。假如说,控件设置了宽度或者高度,那么权重分配的公式如下:
控件1的宽度或者高度=定义的宽度或者高度+1/3(屏幕宽度或者高度-两个控件的宽度或者高度之和)
控件2的宽度或者高度=定义的宽度或者高度+2/3(屏幕宽度或者高度-两个控件的宽度或者高度之和)。
通过公式我们应该就明白了权重分配剩余空间的原理了吧。这里我们要注意一点,假如你是在horizontal的线性布局里面分配权重,那么,高度不能设置为0dp。理解很简单,水平方向的宽度因为你设置了权重,所以宽度由权重来分配。所以宽度设置为0dp系统不会报错。但是高度没人给他分配呀。。所以高度不能设置为0dp。在vertical布局中就反之。
LinearLayout线性布局搭配权重属性的使用的更多相关文章
- Android布局管理详解(1)—— LinearLayout 线性布局
Android的布局方式共有6种,分别是LinearLayout(线性布局).TableLayout(表格布局).FrameLayout(帧布局).RelativeLayout(相对布局).GridL ...
- 2.2.1 LinearLayout(线性布局)
本节引言 本节开始讲Android中的布局,Android中有六大布局,分别是: LinearLayout(线性布局), RelativeLayout(相对布局), TableLayout(表格布局) ...
- Android零基础入门第25节:最简单最常用的LinearLayout线性布局
原文:Android零基础入门第25节:最简单最常用的LinearLayout线性布局 良好的布局设计对于UI界面至关重要,在前面也简单介绍过,目前Android中的布局主要有6种,创建的布局文件默认 ...
- android 59 LinearLayout 线性布局
##常见的布局* LinearLayout 线性布局线性布局往左右拉是拉不动的,> 线性布局的朝向 vertical|horizontal> 线性布局的权重 weight 和 0dip一起 ...
- Android精通:View与ViewGroup,LinearLayout线性布局,RelativeLayout相对布局,ListView列表组件
UI的描述 对于Android应用程序中,所有用户界面元素都是由View和ViewGroup对象构建的.View是绘制在屏幕上能与用户进行交互的一个对象.而对于ViewGroup来说,则是一个用于存放 ...
- LinearLayout线性布局
作用 : 线性布局会将容器中的组件一个一个排列起来, LinearLayout可以控制组件横向或者纵向排列, 通过android:orientation属性控制; 不换行属性 : 线性布局中的组件不会 ...
- Android LinearLayout线性布局
LinearLayout是线性布局控件:要么横向排布,要么竖向排布 决定性属性:必须有的! android:orientation:vertical (垂直方向) .horizontal(水平方向) ...
- LinearLayout 线性布局
android:orientation 设置布局管理器内组件的排列方式,可设置为 horizontal (水平排列).vertical (垂直排列) android:gravity 设置布局管理器内组 ...
- Android LinearLayout线性布局详解
为了更好地管理Android应用的用户界面里的各组件,Android提供了布局管理器.通过使用布局管理器,Android应用图形用户界面具有良好的平台无关性.推荐使用布局管理器来管理组件的分布.大小, ...
随机推荐
- value optimized out的问题
看redis源码,查看某个变量的值的时候出现:value optimized out 变量被编译优化掉了,看不到了. 解决方法: 在编译redis的时候,make添加参数.0表示编译的时候不对代码进行 ...
- js判断是pc还是移动端
//判断pc还是移动端 var isM = function () { var ua = navigator.userAgent; /* navigator.userAgent 浏览器发送的用户代理标 ...
- The tag handler class for "home.jsp" (org.apache.taglibs.standard.tag.rt.core.ForEachTag) was not found on the Java Build Path
web.xml中 listener,filter,servlet需按顺序. <listener> <listener-class>listener.VisitCountList ...
- Python爬虫之BeautifulSoup的用法
之前看静觅博客,关于BeautifulSoup的用法不太熟练,所以趁机在网上搜索相关的视频,其中一个讲的还是挺清楚的:python爬虫小白入门之BeautifulSoup库,有空做了一下笔记: 一.爬 ...
- hdu 1536/ hdu 1944 S-Nim(sg函数)
S-Nim Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- linux IP局域网监控工具——iptraf
iptraf iptraf是一款交互式.色彩鲜艳的IP局域网监控工具.它可以显示每个连接以及主机之间传输的数据量.下面是屏幕截图. $ sudo iptraf 安装iptraf: # Centos(基 ...
- codeforces 755F F. PolandBall and Gifts(贪心+多重背包)
题目链接: F. PolandBall and Gifts time limit per test 1.5 seconds memory limit per test 256 megabytes in ...
- hdu1085 Holding Bin-Laden Captive!(母函数)
简单的母函数应用. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstrin ...
- hdu1398 Square Coins(母函数)
题目类似于整数拆分,很明显用母函数来做. 母函数的写法基本固定,根据具体每项乘式的不同做出一些修改就行了.它的思路是从第一个括号开始,一个括号一个括号的乘开,用c1数组保存之前已经乘开的系数,即c1[ ...
- mysql-jdbc创建connection理解
jdbc源码分析(http://blog.csdn.net/brilliancezhou/article/details/5499738) 创建JDBC连接代码 Class.forName(" ...