借鉴:https://www.jianshu.com/p/4a16d831c574

虽然目前项目里没有遇到过这个功能,但是以防以后遇到后再去上网查,所以在此记录,这个还是比较简单的,直接看代码。

一、依赖

1. 在根目录下(工程Project)的build.gradle中添加

allprojects {repositories {...maven { url "https://jitpack.io" }}
}

2. 在项目(Module)的build.gradle中添加

implementation 'com.github.open-android:labelview:0.1.0'

二、布局实现

1. Button

<com.lid.lib.LabelButtonViewandroid:id="@+id/labelbutton"android:layout_width="200dp"android:layout_height="48dp"android:background="#03a9f4"android:gravity="center"android:text="Button"android:textColor="#ffffff"app:label_backgroundColor="#C2185B"app:label_distance="20dp"app:label_height="20dp"app:label_orientation="RIGHT_TOP"app:label_text="HD"app:label_textSize="12sp"app:label_textStyle="BOLD"/>

2. ImageView

<com.lid.lib.LabelImageViewandroid:id="@+id/image1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:scaleType="centerCrop"android:src="@mipmap/ic_launcher"app:label_backgroundColor="#C2185B"app:label_orientation="LEFT_TOP"app:label_text="CHINA"app:label_textStyle="ITALIC"/>

3. TextView

<com.lid.lib.LabelTextViewandroid:id="@+id/text"android:layout_width="wrap_content"android:layout_height="48dp"android:layout_gravity="center"android:layout_marginTop="8dp"android:background="#212121"android:gravity="center"android:padding="16dp"android:text="TextView"android:textColor="#ffffff"app:label_backgroundColor="#03A9F4"app:label_distance="15dp"app:label_orientation="LEFT_TOP"app:label_text="POP"app:label_textSize="10sp"app:label_textStyle="BOLD_ITALIC"/>

三、属性

1. app:label_backgroundColor="#C2185B"    贴纸颜色

2. app:label_distance="20dp"    距离角落的距离

3. app:label_height="20dp"      贴纸的宽度

4. app:label_text="HD"    贴纸的文本内容

5. app:label_textSize="12sp"    贴纸的文本内容的大小

6. app:label_textStyle="BOLD"     贴纸的文本内容的样式

7. app:label_orientation="RIGHT_TOP"  贴纸的位置

  • LEFT_TOP:代表左上方,为默认值
  • RIGHT_TOP:代表右上方
  • LEFT_BOTTOM:代表左下方
  • RIGHT_BOTTOM:代表右下方

四、高级使用(自定义)

如果我们要在自定义视图中打标签怎么办,其实很简单,一般只需要四步就可以:

  • 创建一个视图类继承你需要打标签视图
  • 使用LabelViewHelper的对象作为新类的成员变量
  • 在构造方法和onDraw方法中调用LabelViewHelper中的方法
  • 在其他相关方法中调用LabelViewHelper中的方法
public class LabelXXXView extends YourView {LabelViewHelper utils;public LabelXXXView(Context context) {this(context, null);}public LabelXXXView(Context context, AttributeSet attrs) {this(context, attrs, 0);}public LabelXXXView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);utils = new LabelViewHelper(context, attrs, defStyleAttr);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);utils.onDraw(canvas, getMeasuredWidth(), getMeasuredHeight());}public void setLabelHeight(int height) {utils.setLabelHeight(this, height);}public int getLabelHeight() {return utils.getLabelHeight();}public void setLabelDistance(int distance) {utils.setLabelDistance(this, distance);}public int getLabelDistance() {return utils.getLabelDistance();}public boolean isLabelVisual() {return utils.isLabelVisual();}public void setLabelVisual(boolean enable) {utils.setLabelVisual(this, enable);}public int getLabelOrientation() {return utils.getLabelOrientation();}public void setLabelOrientation(int orientation) {utils.setLabelOrientation(this, orientation);}public int getLabelTextColor() {return utils.getLabelTextColor();}public void setLabelTextColor(int textColor) {utils.setLabelTextColor(this, textColor);}public int getLabelBackgroundColor() {return utils.getLabelBackgroundColor();}public void setLabelBackgroundColor(int backgroundColor) {utils.setLabelBackgroundColor(this, backgroundColor);}public String getLabelText() {return utils.getLabelText();}public void setLabelText(String text) {utils.setLabelText(this, text);}public int getLabelTextSize() {return utils.getLabelTextSize();}public void setLabelTextSize(int textSize) {utils.setLabelTextSize(this, textSize);}
}

当然,我们对自定义View打标签并不总是按上面的方法来进行的。
比如,我要给EditText打标签,按照上面的方法来,结果是这样的

我们发现下面的横线不见了,怎么回事啊。
这是因为EditText一般没有自定义属性,就调用了下面这个构造方法:

public EditText(Context context, AttributeSet attrs) {this(context, attrs, com.android.internal.R.attr.editTextStyle);}

可以看到这个构造方法里面自动给它添加了一个com.android.internal.R.attr.editTextStyle样式,正是这个样式才使EditText下面有横线。而按照上面的四步来,我们就丢失了这个样式,现在我们想要这个样式怎么办。别担心,很简单,在原来四步的基础上保持不变,修改第三个构造方法就可以了,将

public LabelEditView(Context context, AttributeSet attrs, int defStyleAttr) {super(context,attrs,defStyleAttr);utils = new LabelViewHelper(context, attrs, defStyleAttr);}

修改为:

public LabelEditView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs);utils = new LabelViewHelper(context, attrs, defStyleAttr);}

我们来看看修改后的界面:

对于这两种情况,xml中的定义是一样的:

<com.example.zyt.mylabelview.LabelEditTextViewapp:label_backgroundColor="#03A9F4"app:label_distance="15dp"app:label_orientation="RIGHT_TOP"app:label_text="微信"app:label_textSize="10sp"android:layout_width="200dp"android:layout_height="48dp" />

Android中简单实现贴纸功能labelview相关推荐

  1. android java 8_四个库,让你在 Android 中启用 Java 8 功能

    Java 8 的推出引入很多革命性变化,加入了函数式编程的特征,使基于行为的编程成为可能,同时简化了各种设计模式的实现方式,是 Java 有史以来最重要的更新. 自 Android N 之后,由于 J ...

  2. Android实现简单的计算器功能

    Android实现简单的计算器功能 **前言:**通过Android实现简单的计算器功能,实现简单的加.减.乘.除操作. 效果图如下: 第一步 布局文件:activity_main.xml <? ...

  3. android实现简单的记账功能

    android实现简单的记账功能 ps:这次作业真的有点折磨我,花了我断断续续三天的时间,其中有很多功能都是想实现,但是感觉就差一点点,由于截止时间快要到了,只能将还未实现的功能先搁置,后续会补充更多 ...

  4. android 碎片技术,【移动开发】Android中强大的适配功能----Fragment(碎片)总结

    [移动开发]Android中强大的适配功能----Fragment(碎片)总结 发布时间:2020-06-27 00:32:58 来源:51CTO 阅读:10233 作者:zhf651555765 作 ...

  5. Java乔晓松-android中调用系统拍照功能并显示拍照的图片

    android中调用系统拍照功能并显示拍照的图片 如果你是拍照完,利用onActivityResult获取data数据,把data数据转换成Bitmap数据,这样获取到的图片,是拍照的照片的缩略图 代 ...

  6. Android中复制及粘贴功能

    工作中用到了Android的Copy and Paste功能,上官网http://developer.android.com/guide/topics/text/copy-paste.html#Cli ...

  7. Android中实现一键分享功能

    在Android中开发中,我们会经常遇到使用一键分享的功能,今天我以使用第三方平台ShareSDK来实现一键分享向大家分享下具体的流程. 下面,我就以步骤顺序依次展示在自己的Android项目中如何实 ...

  8. android中最新webview的功能,Android WebView实现截长图功能

    本文实例为大家分享了Android实现截长图功能的具体代码,供大家参考,具体内容如下 先看看手机自带的长截屏功能:  机型: vivo x9 plus 大胆推测实现逻辑: 1:需要一个可以滚动的Vie ...

  9. Android 中 简单商品列表 的运用

     可参考Android 中对 ListView 中 进行增删改查 完成效果如图所示: GoodActivity package com.example.sqlitedemo;import androi ...

最新文章

  1. java用画线写字_用Java画线
  2. 我决定重新开始搞机器学习啦
  3. Java迭代器Iterator
  4. [Vue源码分析]自定义事件原理及事件总线的实现
  5. html鼠标悬停盒子凸起,css3 transform动画鼠标悬停div容器凸起放大显示
  6. mocha 测试 mysql_GitHub - zouzhenxing/lei: 整合Express mysql ioredis ejs 的一开发框架,使用mocha对api进行测试...
  7. float right不生效_【工具篇】程序员不愿意写 PPT 是姿势不对?
  8. 编译程序 解释程序
  9. 【图像去雾】基于matlab直方图+retinex+暗通道图像去雾【含Matlab源码 074期】
  10. C语言例题:数字重组
  11. 中国计算机学会(CCF)推荐国际学术会议和期刊目录(2019年版,官网转载)
  12. win10怎么设置锁定计算机,win10电脑自动锁屏怎么设置_让win10电脑自动锁屏的设置方法...
  13. java对接苹果支付
  14. 设备备件管理怎么做?
  15. opencv 打开摄像头
  16. 最凄美的爱情故事,让每对爱人更长久的文章!!
  17. IOS navigationController详解
  18. windows安装MongoDB,Robo 3T
  19. 运筹优化(十四)--离散优化的启发式算法
  20. where 1=1 是什么有意思

热门文章

  1. mbp 外接显示器 视网膜_视网膜显示已准备就绪,可进行媒体查询
  2. windows10 chrome 安装crx插件
  3. 企业即时通讯如何让企业沟通变得简单
  4. 3.2 51单片机-蜂鸣器模块
  5. 假扮90后骗900万,这个骗局的背后隐藏一个赚钱的行业
  6. ArcGIS10.2详细安装教程(附安装包获取方式)
  7. 秋季喝什么茶对身体好
  8. 十年测试两茫茫,不思量,自难忘!
  9. 第五讲 一元函数微分学的几何应用
  10. pdmreader使用