android weight代码,Android:weight,margin,padding详解实例

6d0cb88695dd?utm_medium=timeline&utm_source=oschina-app

标题图

weight详解

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

案例代码

6d0cb88695dd?utm_medium=timeline&utm_source=oschina-app

dashucoding

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">

android:layout_width="0dp"

android:layout_height="match_parent"

android:background="#ADFF2F"

android:layout_weight="1"/>

android:layout_width="0dp"

android:layout_height="match_parent"

android:background="#DA70D6"

android:layout_weight="2"/>

6d0cb88695dd?utm_medium=timeline&utm_source=oschina-app

dashucoding

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">

android:layout_width="0dp"

android:layout_height="match_parent"

android:background="#ADFF2F"

android:layout_weight="1"/>

android:layout_width="0dp"

android:layout_height="match_parent"

android:background="#DA70D6"

android:layout_weight="1"/>

6d0cb88695dd?utm_medium=timeline&utm_source=oschina-app

dashucoding

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" >

android:layout_weight="1"

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:text="one"

android:background="#98FB98"

/>

android:layout_weight="2"

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:text="two"

android:background="#FFFF00"

/>

android:layout_weight="3"

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:text="three"

android:background="#FF00FF"

/>

6d0cb88695dd?utm_medium=timeline&utm_source=oschina-app

dashucoding

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" >

android:layout_weight="1"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="one"

android:background="#98FB98"

/>

android:layout_weight="2"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="two"

android:background="#FFFF00"

/>

android:layout_weight="3"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="three"

android:background="#FF00FF"

/>

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

每个宽度为match_parent,屏幕为1,那么屏幕就是1-3=-2个math_parent

计算方法,1 - 2 * (1/6) = 2/3,1 - 2 * (2/6) = 1/3, 1 - 2 * (3/6) = 0,结果是one占了两份,two占了一份,three什么都没有

margin,padding详解

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

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:id="@+id/btn1"

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:text="Button"/>

android:paddingLeft="100dp"

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:text="Button"

android:layout_toRightOf="@id/btn1"/>

android:id="@+id/btn2"

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:text="Button"

android:layout_alignParentBottom="true"/>

android:layout_marginLeft="100dp"

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:text="Button"

android:layout_toRightOf="@id/btn2"/>

结语

本文主要讲解 Android:weight,margin,padding详解实例

下面我将继续对其他知识 深入讲解 ,有兴趣可以继续关注

小礼物走一走 or 点赞

6d0cb88695dd?utm_medium=timeline&utm_source=oschina-app

送❤

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值