一: 實(shí)現(xiàn)的目標(biāo)
點(diǎn)擊Android 中Tablayout 的各個(gè)Tab時(shí),有ripple(水波紋)效果,并且水波紋范圍并不局限于Tab的寬高,即下面的無界波紋
二:解決探索:
經(jīng)過Google 發(fā)現(xiàn),Android 5.0 以上 ripple 效果的使用大致為以下[1]
所以使用Ripple的關(guān)鍵就在android:backgroud中設(shè)置。
點(diǎn)擊效果主要分為2類:
?一 : 有邊界波紋:寫法如下
android:background="?android:attr/selectableItemBackground"
?二 : 無邊界波紋(波紋的大小不局限于目標(biāo)View的大小):寫法如下
android:background="?android:attr/selectableItemBackgroundBorderless"
原本想,這么簡單,直接設(shè)置到Tablayout的 Tab 的 CustomView 中的根布局就好了。
結(jié)果發(fā)現(xiàn),這么做都不行。ripple的效果是有了,但是ripple的范圍是局限在每個(gè)Tab的大小之內(nèi)
最后發(fā)現(xiàn)這種做法不可行。懷疑與 Tablayout 有關(guān),最后google發(fā)現(xiàn)可以通過如下方法解決[2]:
主要是設(shè)置在TabLayout 的??app:tabBackground="?attr/selectableItemBackground"?? 屬性上
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
app:tabBackground="?attr/selectableItemBackground"
android:layout_height="wrap_content"/>
親測可以。
[2].?https://stackoverflow.com/questions/31605514/tablayout-highlite-and-ripple-effect