TextView + 背景設置, 產生自定義Tab選中效果

效果圖如下所示:


第二個點擊是這樣的:



因此這是一個個性化定製比較強的Tab, 值得擁有, 關鍵要實現的地方如下:

第一: Layout中的佈局

 <TextView android:layout_gravity="center_vertical" android:background="@drawable/selector_food_cls"
   android:layout_width="match_parent" android:layout_weight="1"
   android:layout_height="wrap_content"
   android:clickable="true"
   android:focusable="true"
   android:focusableInTouchMode="true"
   android:id="@+id/veg_id"
   android:text="蔬菜" />

上述紅色部分非常關鍵,將影響到Selector的控制, 接下來重要部分就是Selector了:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_focused="true" android:drawable="@drawable/nav_hover" />
    <item android:state_pressed="true" android:drawable="@drawable/nav_hover"/>
    <item android:drawable="@drawable/nav_a" />
</selector> 

至此,問題完美解決!


完整代碼,請查看




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章