標籤:android tab
最近想修改ActionBar.Tab對象的文本字型和顏色
發現該對象提供的介面非常有限
而網上搜尋關於ActionBar.Tab結果大多比較早 使用的也不是google提供的例子中的ActionBar
於是自己研究了一天 找到了使用介面setCustomView來自訂ActionBar.Tab對象
先通過ActionBar.getTabAt(int) 獲得一個ActionBar.Tab對象
然後調用setCustomView 使用自訂的layout
注意這裡沒有生產layout的對象 而是直接使用該布局
布局可以定製好設定文字大小 背景 顏色
後面就和使用普通布局一樣
獲得一個TextView 再設定文本 背景 等等
actionBar = getSupportActionBar();ActionBar.Tab t = actionBar.getTabAt(i);t.setCustomView(R.layout.tab_layout_1);TextView title = (TextView)t.getCustomView().findViewById(R.id.tab_title);title.setBackgroundResource(R.drawable.detail);title.setText(sectionID);
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/tab_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:layout_gravity="center_vertical|center" android:textColor="@android:color/holo_blue_dark" android:textSize="14sp" /></FrameLayout>