記:用ConstraintLayout實(shí)現(xiàn)消息小紅點(diǎn)

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ō)明圖:


效果圖:


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容