ConstraintLayout翻譯為約束布局,也有人把它稱作增強(qiáng)型的相對(duì)布局,由 2016 年 Google I/O 推出。扁平式的布局方式,無(wú)任何嵌套,減少布局的層級(jí),優(yōu)化渲染性能。從支持力度而言,將成為主流布局樣式,完全代替其他布局。
消息小紅點(diǎn)的實(shí)現(xiàn)方式有很多,比如在RelativeLayout中也可以,但是比較復(fù)雜,需要計(jì)算padding或margin,并且可能還不精確,下面就用ConstraintLayout實(shí)現(xiàn)消息小紅點(diǎn)
首先是定義一個(gè)顯示的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個(gè)屬性,保證該View居中
然后就是顯示消息數(shù)字的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,因?yàn)榧t點(diǎn)顯示肯定是要依附在另一個(gè)View上顯示的
? ??????layout_constraintCircleAngle:偏移圓角度 水平右方向?yàn)?逆時(shí)針?lè)较蛐D(zhuǎn),45表示向右旋轉(zhuǎn)45度
? ??????layout_constraintCircleRadius:圓半徑,可以理解為紅點(diǎn)位置和所依附的View的距離,以此來(lái)
確定紅點(diǎn)View的位置
引用一張說(shuō)明圖:

效果圖:
