Android ConstraintLayout 实现角标

ConstraintLayout + Placeholder 实现

UI布局中常见的角标数量的设计如下图:
image

类似于这种的设计,之前的做法是 上下左右四边约束,使其角标中心位于底部view的右上角

但是等到UI还原的时候设计小哥就提出了问题,说角标偏外了,需要往里挪一挪

。。。。。。

我的实现方案

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_round_8"
        android:padding="@dimen/padding_10"
        android:text="Hello World!"
        android:textColor="@color/white"
        android:textSize="@dimen/size_20"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/text_jb"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:background="@drawable/bg_round_8_red"
        android:gravity="center"
        android:text="9"
        android:textColor="@color/white"
        app:layout_constraintBottom_toTopOf="@id/zhan_wei"
        app:layout_constraintEnd_toEndOf="@id/zhan_wei"
        app:layout_constraintStart_toEndOf="@id/zhan_wei"
        app:layout_constraintTop_toTopOf="@id/zhan_wei" />

    <androidx.constraintlayout.widget.Placeholder
        android:id="@+id/zhan_wei"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="8dp"
        app:layout_constraintBottom_toBottomOf="@id/text"
        app:layout_constraintLeft_toLeftOf="@id/text"
        app:layout_constraintRight_toRightOf="@id/text"
        app:layout_constraintTop_toTopOf="@id/text" />

</androidx.constraintlayout.widget.ConstraintLayout>

需要往里或者外挪多少只需要调整占位的margin 的就行了
思路就是改变角标的参照view

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前端常用英语词汇表 A abstraction 抽象、抽象物、抽象性 access 存取、访问 account 账...
    月子心阅读 750评论 0 2
  • 今天是什么日子 起床:5:50 就寝:11:00 天气:阴 心情:小激动加小焦虑 纪念日:2.0正式启动 任务清单...
    子木马阅读 125评论 0 0
  • “双减”政策下的物理教研 校长特别强调,“双减”政策的出台对我们教研、课堂教学和作业的设计提出了更高的要求。让课堂...
    6f121ba288ce阅读 83评论 0 1
  • 1. 宝宝现在可会用词了,中午和姥姥睡觉,和姥姥说:“我觉得有点远”。 2. 下午宝宝睡醒,领他去蓝姆氏幼儿园溜达...
    淞淞阅读 120评论 0 0
  • 【R:阅读原文】片段选自《跃迁》p122-123 【主要内容】极其功利地配置资源 《如何阅读一本书》中,给...
    凝珀Mok阅读 258评论 3 1