前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android:weight,margin,padding详解实例

Android:weight,margin,padding详解实例

作者头像
达达前端
发布2019-07-03 12:27:21
4780
发布2019-07-03 12:27:21
举报
文章被收录于专栏:达达前端达达前端

标题图

weight详解

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

案例代码

dashucoding

代码语言:javascript
复制
<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>

dashucoding

代码语言:javascript
复制
<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>

dashucoding

代码语言:javascript
复制
<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>

dashucoding

代码语言:javascript
复制
<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代表是填充,填充的对象针对的是组件中的元素。

代码语言:javascript
复制
<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 点赞
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.08.19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • weight详解
    • 案例代码
      • 为什么出现2:1的案例呢?three怎么就不见了呢?
      • margin,padding详解
      • 结语
      相关产品与服务
      容器服务
      腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档