设为首页收藏本站

IT技术擎 - 最棒的IT web技术交流社区

 找回密码
 注册为IT技术擎人

QQ登录

只需一步,快速开始

搜索
热搜: php h5 jquery
查看: 24|回复: 0

[未分类] Android:weight,margin,padding详解实例

[复制链接]
发表于 2018-9-5 00:18:19 | 显示全部楼层 |阅读模式

weight详解
  1. weight
复制代码
是用来等比例划分区域的属性。
案例代码
  1. <LinearLayout
  2. xmlns:android= "http://schemas.android.com/apk/res/android "
  3. xmlns:tools= "http://schemas.android.com/tools "
  4. android:id= "@+id/LinearLayout "
  5. android:layout_width= "match_parent "
  6. android:layout_height= "match_parent "
  7. android:orientation= "horizontal " >
  8. <LinearLayout
  9. android:layout_width= "0dp "
  10. android:layout_height= "math_parent "
  11. android:background= "#ADFF2F "
  12. android:layout_weight= "1 "/ >
  13. <LinearLayout
  14. android:layout_width= "0dp "
  15. android:layout_height= "math_parent "
  16. android:background= "#DA70D6 "
  17. android:layout_weight= "2 "/ >
  18. </LinearLayout >
复制代码
  1. <LinearLayout
  2. xmlns:android= "http://schemas.android.com/apk/res/android "
  3. xmlns:tools= "http://schemas.android.com/tools "
  4. android:id= "@+id/LinearLayout "
  5. android:layout_width= "match_parent "
  6. android:layout_height= "match_parent "
  7. android:orientation= "horizontal " >
  8. <LinearLayout
  9. android:layout_width= "0dp "
  10. android:layout_height= "math_parent "
  11. android:background= "#ADFF2F "
  12. android:layout_weight= "1 "/ >
  13. <LinearLayout
  14. android:layout_width= "0dp "
  15. android:layout_height= "math_parent "
  16. android:background= "#DA70D6 "
  17. android:layout_weight= "1 "/ >
  18. </LinearLayout >
复制代码
  1. <LinearLayout
  2. 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 " >
  3. <TextView android:layout_weight= "1 " android:layout_width= "wrap_content " android:layout_height= "math_parent " android:text= "one " android:background= "#98FB98 " / > <TextView android:layout_weight= "2 " android:layout_width= "wrap_content " android:layout_height= "math_parent " android:text= "two " android:background= "#FFFF00 " / > <TextView android:layout_weight= "3 " android:layout_width= "wrap_content " android:layout_height= "math_parent " android:text= "three " android:background= "#FF00FF " / >
  4. </LinearLayout >
复制代码
  1. <LinearLayout
  2. 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 " >
  3. <TextView android:layout_weight= "1 " android:layout_width= "math_parent " android:layout_height= "math_parent " android:text= "one " android:background= "#98FB98 " / > <TextView android:layout_weight= "2 " android:layout_width= "math_parent " android:layout_height= "math_parent " android:text= "two " android:background= "#FFFF00 " / > <TextView android:layout_weight= "3 " android:layout_width= "math_parent " android:layout_height= "math_parent " android:text= "three " android:background= "#FF00FF " / >
  4. </LinearLayout >
复制代码
为什么出现2:1的案例呢?three怎么就不见了呢?

  • 每个宽度为
    1. math_parent
    复制代码
    ,屏幕为1,那么屏幕就是1-3=-2个
    1. math_parent
    复制代码
  • 计算方法,
    1. 1 - 2 * (1/6) = 2/3,1 - 2 * (2/6) = 1/3, 1 - 2 * (3/6) = 0
    复制代码
    ,结果是one占了两份,two占了一份,three什么都没有
  1. margin
复制代码
  1. padding
复制代码
详解
  1. margin
复制代码
代表是偏移,表示组件离容器偏移量,
  1. margin
复制代码
针对的是容器中的组件元素;
  1. padding
复制代码
代表是填充,填充的对象针对的是组件中的元素。
  1. <RelativeLayout
  2. 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 " >
  3. <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 "/ >
  4. <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 "/ >
  5. </RelativeLayout >
复制代码
结语

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





上一篇:java程序员的从0到1:@Resource与@Autowired的比较
下一篇:java8 十大新特性
该用户未在地球留下任何的痕迹

本版积分规则

QQ|小黑屋|帮助|IT技术擎 ( 沪ICP备15054863号  

GMT+8, 2018-11-19 06:57

Powered by Discuz! X3.2 Licensed

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表