博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android margin--负的margin的使用
阅读量:7040 次
发布时间:2019-06-28

本文共 3016 字,大约阅读时间需要 10 分钟。

通常情况下,如果我们想要两个控件实现重叠的效果,一般都是使用FrameLayout 或者RelativeLayout布局。其实,如果设置两个控件的margin值为负数,也能实显控件重叠的效果。

先展示各种效果图:

示例代码1–对应上图中的1:

<LinearLayout

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">

<TextView

android:layout_width="180dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:background="#00ff00"
android:padding="-20dp"
android:text="这是没加margin值的效果"/>
<TextView
android:layout_width="180dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:background="#678df7"
android:padding="10dp"
android:text="这是没加margin值的效果"/>
</LinearLayout>
示例代码2 –对应上图中的2

<LinearLayout

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">

<TextView

android:layout_width="180dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:background="#00ff00"
android:padding="-20dp"
android:text="这是正的margin值的效果"/>
<TextView
android:layout_width="180dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_marginLeft="30dp"
android:background="#678df7"
android:padding="10dp"
android:text="这是正的margin值的效果"/>
</LinearLayout>
示例代码3 –对应上图中3 的效果

<LinearLayout

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">

<TextView

android:layout_width="180dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:background="#00ff00"
android:padding="-20dp"
android:text="这是加了负的margin值的效果"/>

<!--通过这里设置的负的margin值,实现了左侧tv覆盖住右侧tv一部分的效果-->

<TextView
android:layout_width="180dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_marginLeft="-30dp"
android:background="#678df7"
android:padding="10dp"
android:text="这是加了负的margin值的效果"/>
</LinearLayout>
示例代码4 –对应上图中的效果4

<!--消息提示的实现方式1 ,这种应该是实现未读消息提醒布局最简单的写法-->

<RelativeLayout
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginTop="20dp"
android:background="@drawable/square_about_me">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/red"
android:gravity="center"
android:text="99+"/>
</RelativeLayout>
示例代码5 –对应上图中的5

<!--消息提示的实现方式2,通过使用这种负的margin值的写法,就可以灵活的调整右上角小红点的位置-->

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_marginTop="20dp">
<ImageView
android:id="@+id/iv1"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/square_about_me"/>

<!--通过设置负的margin值,实现小红点覆盖到小铃铛上的效果,并且可以通过调整margin值来调整小铃铛的位置-->

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="-30dp"
android:layout_toRightOf="@id/iv1"
android:background="@drawable/red"
android:gravity="center"
android:text="99+"/>
</RelativeLayout>
---------------------

原文:https://blog.csdn.net/north1989/article/details/52922564 

转载于:https://www.cnblogs.com/porter/p/10735921.html

你可能感兴趣的文章
判断ie浏览器7、8、9三个版本
查看>>
GDUFE ACM-1124
查看>>
Schwarz积分公式
查看>>
工作中常用的 Linux 命令
查看>>
English Corner
查看>>
(最短路 SPFA)Invitation Cards -- poj -- 1511
查看>>
两数相加LeetCode
查看>>
列表生成 加1四种方法
查看>>
springboot 处理后端long传给前端精度丢失问题
查看>>
[译文]扩展Repeater控件以支持DataPager分页
查看>>
88. Merge Sorted Array
查看>>
java抽象类和接口区别
查看>>
构建Ruby开发环境(Windows+Eclipse+Aptana Plugin)
查看>>
Miao Xian 隐私政策
查看>>
三维实景下的南极科考站是什么样子?
查看>>
Linux利用scp命令来进行文件复制
查看>>
【LabVIEW技巧】你可以不懂OOP,却不能不懂封装
查看>>
《Programming in Lua 3》读书笔记(十五)
查看>>
PHP读取xlsx Excel 文件
查看>>
R语言模型中的加总偏误与内生性:一种数值模拟方法
查看>>