weight详解

weight是用来等比例划分区域的属性。

案例代码

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"> <LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#ADFF2F"
android:layout_weight="1"/> <LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#DA70D6"
android:layout_weight="2"/> </LinearLayout>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"> <LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#ADFF2F"
android:layout_weight="1"/> <LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#DA70D6"
android:layout_weight="1"/> </LinearLayout>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" > <TextView
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="one"
android:background="#98FB98"
/>
<TextView
android:layout_weight="2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="two"
android:background="#FFFF00"
/>
<TextView
android:layout_weight="3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="three"
android:background="#FF00FF"
/> </LinearLayout>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" > <TextView
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="one"
android:background="#98FB98"
/>
<TextView
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="two"
android:background="#FFFF00"
/>
<TextView
android:layout_weight="3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="three"
android:background="#FF00FF"
/> </LinearLayout>

为什么出现2:1的案例呢?three怎么就不见了呢?

  1. 每个宽度为match_parent,屏幕为1,那么屏幕就是1-3=-2个math_parent
  2. 计算方法,1 - 2 * (1/6) = 2/3,1 - 2 * (2/6) = 1/3, 1 - 2 * (3/6) = 0 ,结果是one占了两份,two占了一份,three什么都没有

marginpadding详解

margin代表是偏移,表示组件离容器偏移量,margin针对的是容器中的组件元素;padding代表是填充,填充的对象针对的是组件中的元素。

<RelativeLayout
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"> <Button
android:id="@+id/btn1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Button"/>
<Button
android:paddingLeft="100dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Button"
android:layout_toRightOf="@id/btn1"/> <Button
android:id="@+id/btn2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Button"
android:layout_alignParentBottom="true"/>
<Button
android:layout_marginLeft="100dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Button"
android:layout_toRightOf="@id/btn2"/> </RelativeLayout>

结语

  • 本文主要讲解 Android:weight,margin,padding详解实例
  • 下面我将继续对其他知识 深入讲解 ,有兴趣可以继续关注
  • 小礼物走一走 or 点赞

Android:weight,margin,padding详解实例的更多相关文章

  1. Android中mesure过程详解

    我们在编写layout的xml文件时会碰到layout_width和layout_height两个属性,对于这两个属性我们有三种选择:赋值成具体的数值,match_parent或者wrap_conte ...

  2. 《Android NFC 开发实战详解 》简介+源码+样章+勘误ING

    <Android NFC 开发实战详解>简介+源码+样章+勘误ING SkySeraph Mar. 14th  2014 Email:skyseraph00@163.com 更多精彩请直接 ...

  3. Android开发之InstanceState详解

    Android开发之InstanceState详解   本文介绍Android中关于Activity的两个神秘方法:onSaveInstanceState() 和 onRestoreInstanceS ...

  4. Message,MessageQueue,Looper,Handler详解+实例

    Message,MessageQueue,Looper,Handler详解+实例 原文地址 Android的Handler使用(这篇简单介绍Handler的使用) 一.几个关键概念 1.Message ...

  5. iOS开发——UI篇OC篇&UICollectionView详解+实例

    UICollectionView详解+实例 实现步骤: 一.新建两个类 1.继承自UIScrollView的子类,比如HMWaterflowView * 瀑布流显示控件,用来显示所有的瀑布流数据 2. ...

  6. android shape的使用详解以及常用效果(渐变色、分割线、边框、半透明阴影效果等)

    shape使用.渐变色.分割线.边框.半透明.半透明阴影效果. 首先简单了解一下shape中常见的属性.(详细介绍参看  api文档 ) 转载请注明:Rflyee_大飞: http://blog.cs ...

  7. Android:TextView跑马灯-详解

    Android:TextView跑马灯_详解 引言: TextView之所以需要跑马灯,是由于文字太长,或者是吸引眼球. 关键代码如下: android:singleLine="true&q ...

  8. Android开发之InstanceState详解(转)---利用其保存Activity状态

    Android开发之InstanceState详解   本文介绍Android中关于Activity的两个神秘方法:onSaveInstanceState() 和 onRestoreInstanceS ...

  9. Xamarin android CardView的使用详解

    android 5.0新增加的一个控件CardView,在support v7兼容包中,意思就是卡片View,虽然可以设置阴影,圆角等等样式,但是我们也可以自己写出来,谷歌工程师之所以出这个,肯定是帮 ...

随机推荐

  1. java.util.Stack类中的peek()方法

    java.util.stack类中常用的几个方法:isEmpty(),add(),remove(),contains()等各种方法都不难,但需要注意的是peek()这个方法. peek()查看栈顶的对 ...

  2. 剑指offer例题——用两个栈实现队列

    题目:用两个栈来实现一个队列,完成队列的Push和Pop操作.队列中的元素为int类型. 首先是概念理解,栈和对列存取的区别 栈(stack)是一种后进先出(last in first out, LI ...

  3. python入门 -- 学习笔记1

    学习资料:笨方法学Python 准备: 安装环境----请自行网络搜索(Windows安装很简单,和其他安装程序一样) 找一个自己习惯的编辑器(比如:sublime text 3) 创建一个专门的目录 ...

  4. centos7下面添加htop工具

    htop下载wget http://sourceforge.net/projects/htop/files/latest/download 解压tar -zxf downloadcd htop-1.0 ...

  5. cobbler的ks文件带有特殊字符导致错误

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAykAAACWAQMAAADg0FUUAAAAAXNSR0IArs4c6QAAAAZQTFRFq6urAA

  6. gcc 与 g++的区别

    原文: http://www.cnblogs.com/wb118115/p/5969775.html ------------------------------------------------- ...

  7. CSS文本超出指定行数省略显示

    单行: overflow: hidden; text-overflow: ellipsis; white-space: nowrap; 2行: font-size: 17px;overflow: hi ...

  8. 什么是XML?

    XML被设计用来传输和存储数据. HTML被设计用来显示数据. 什么是XML? XML指可扩展标记语言(EXtensible Markup Language) XML是一种标记语言,很类似HTML X ...

  9. F4 help for month

    INCLUDE rmcs0f0m. s_month FOR s001-spmon NO-EXTENSION NO INTERVALS OBLIGATORY. AT SELECTION-SCREEN O ...

  10. php redis pipeline用法

    $redis_110 = Redis::connection('redis_110');$redis_85 = Redis::connection('redis_85');$zadd_key_arr ...