记:用ConstraintLayout实现消息小红点

ConstraintLayout翻译为约束布局,也有人把它称作增强型的相对布局,由 2016 年 Google I/O 推出。扁平式的布局方式,无任何嵌套,减少布局的层级,优化渲染性能。从支持力度而言,将成为主流布局样式,完全代替其他布局。

消息小红点的实现方式有很多,比如在RelativeLayout中也可以,但是比较复杂,需要计算padding或margin,并且可能还不精确,下面就用ConstraintLayout实现消息小红点

首先是定义一个显示的View,如

<TextView

    android:id="@+id/tv_textView"

    android:layout_width="50dp"

    android:layout_height="50dp"

    android:background="@color/colorPrimary"

    android:gravity="center"

    android:text="文本"

    android:textColor="#fff"

    app:layout_constraintBottom_toBottomOf="parent"

    app:layout_constraintLeft_toLeftOf="parent"

    app:layout_constraintRight_toRightOf="parent"

    app:layout_constraintTop_toTopOf="parent" />

注:layout_constraintBottom_toBottomOf(控件的下方距离parent的下方)及剩下的3个属性,保证该View居中

然后就是显示消息数字的View

<TextView

    android:layout_width="20dp"

    android:layout_height="20dp"

    android:background="@drawable/round_shape"

    android:gravity="center"

    android:text="11"

    android:textColor="#fff"

    app:layout_constraintCircle="@id/tv_textView"

    app:layout_constraintCircleAngle="45"

    app:layout_constraintCircleRadius="35dp" />

注:layout_constraintCircle:引用的控件ID,因为红点显示肯定是要依附在另一个View上显示的

        layout_constraintCircleAngle:偏移圆角度 水平右方向为0逆时针方向旋转,45表示向右旋转45度

        layout_constraintCircleRadius:圆半径,可以理解为红点位置和所依附的View的距离,以此来

确定红点View的位置

引用一张说明图:


效果图:


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容