Android 添加标签;角标;控件角落添加Tip

使用LabelView
是在github上一个开源的标签库。其项目主页是:https://github.com/linger1216//labelview 

 

在Androidstudio里面使用:

在项目级别的build.gradle:

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

在app的build.gradle:

dependencies {
	compile 'com.github.linger1216:labelview:v1.1.2'
}

提供带标签的控件:

(这几个的属性label_text,设置的文字因为在studio预览都没有显示出来,程序启动后都会显示出来)

对应的LabelButtonView:带角标的Button;

对应的LabelImageView:带角标的ImageView;

对应的LabelTextView:带角标的TextView;

上面效果的几个代码是:

LabelButtonView:

<com.lid.lib.LabelButtonView
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toSartOf="@id/image"
    app:layout_constraintBottom_toBottomOf="parent"
    android:id="@+id/button"
    android:layout_width="150dp"
    android:layout_height="48dp"
    android:background="#03a9f4"
    android:gravity="center"
    android:text="我是按钮"
    android:textColor="#ffffff"
    app:label_backgroundColor="#C2185B"
    app:label_distance="20dp"
    app:label_height="20dp"
    app:label_orientation="RIGHT_TOP"
    app:label_text="哈哈"
    app:label_textColor="#FFFFFF"
    app:label_textSize="12sp"
    />

LabelImageView:

<com.lid.lib.LabelImageView
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    android:id="@+id/image"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:scaleType="centerCrop"
    android:src="@mipmap/ic_launcher_round"
    android:background="#03a9f4"
    app:label_backgroundColor="#C2185B"
    app:label_orientation="LEFT_TOP"
    app:label_distance="30dp"
    app:label_height="30dp"
    app:label_textColor="#FFFFFF"
    app:label_text="嘿嘿" />

LabelTextView:

<com.lid.lib.LabelTextView
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:padding="16dp"
    android:text="TextView"
    android:textColor="#ffffff"
    app:label_backgroundColor="#C2185B"
    app:label_distance="15dp"
    app:label_orientation="LEFT_TOP"
    app:label_text="嗯嗯"
    app:label_textSize="10sp" />

其中

label_backgroundColor:是标签的背景色,
label_height:标签的高度,也可以说是标签的厚度。。。
label_orientation:标签的位置,分为左上、左下、右上、右下。分别对应:LEFT_TOP,LEFT_BOTTOM,RIGHT_TOP,RIGHT_BOTTOM
label_text:标签的显示文字
label_textColor:标签文字的颜色
label_textSize:标签文字的大小
label_distance:是标签的距离:为0时也就是被label背景色全覆盖,不留白

 

也可以把某个LabelButtonView、LabelImageView、LabelTextView当成单独的控件使用,我是这么使用的:

我的RecycleView的item布局需要打标签,所以:

<com.lid.lib.LabelTextView
        android:layout_alignParentRight="true"
        android:id="@+id/item_overdue_tip"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:padding="0dp"
        android:text=""
        android:textColor="#ffffff"
        app:label_backgroundColor="#03A9F4"
        app:label_distance="13dp"
        app:label_orientation="RIGHT_TOP"
        app:label_text="Tip"
        app:label_textColor ="#FFFFFF"
        app:label_textSize="10sp" />

也是正常使用,调整好相对于父控件的位置就好~

 

 

End---------------------

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果您是想在 Android 应用程序的右上角添加一个标签,您可以使用 Toolbar 控件并在其中添加一个 Menu。在菜单中,您可以添加一个 MenuItem 并设置它的图标和文本。这个 MenuItem 就可以作为右上角的标签来使用。 以下是一个示例代码片段,可以帮助您实现这个功能: ```xml <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay"> <Menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"> <item android:id="@+id/action_label" android:title="Label" android:icon="@drawable/ic_label" app:showAsAction="always" /> </Menu> </android.support.v7.widget.Toolbar> ``` 在这个示例中,我们使用了一个 Toolbar 控件,并在其中添加了一个 Menu。在 Menu 中,我们添加了一个 MenuItem,并设置了它的图标和文本,同时将它的 showAsAction 属性设置为 always,这样这个 MenuItem 就会一直显示在 Toolbar 的右侧。 在您的 Activity 或 Fragment 中,您可以通过以下代码来处理这个 MenuItem 的点击事件: ```java @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_label: // 处理标签点击事件 return true; default: return super.onOptionsItemSelected(item); } } ``` 这样,当用户点击标签时,onOptionsItemSelected() 方法会被调用,并且您可以在其中处理相应的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值