Android: 自定义Tab样式
生活随笔
收集整理的這篇文章主要介紹了
Android: 自定义Tab样式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.制作4個9patch的tab樣式,可參考android默認的資源
tab_unselected.9.png?tab_selected.9.pngtab_press.9.pngtab_focus.9.png
這4個資源分別代表Tab的4種狀態。
2.定義Tab的selector樣式(就叫它tab_indicator.xml好了),將其放入drawable文件夾下,代碼如下:
?
<?xml?version="1.0"?encoding="utf-8"?>??
<selector?xmlns:android="http://schemas.android.com/apk/res/android">??
????<!--?Non?focused?states?-->??
????<item?android:state_focused="false"?android:state_selected="false"?android:state_pressed="false"?android:drawable="@drawable/tab_unselected"?/>??
????<item?android:state_focused="false"?android:state_selected="true"?android:state_pressed="false"?android:drawable="@drawable/tab_selected"?/>??
????<!--?Focused?states?-->??
????<item?android:state_focused="true"?android:state_selected="false"?android:state_pressed="false"?android:drawable="@drawable/tab_focus"?/>??
????<item?android:state_focused="true"?android:state_selected="true"?android:state_pressed="false"?android:drawable="@drawable/tab_focus"?/>??
????<!--?Pressed?-->??
????<item?android:state_pressed="true"?android:drawable="@drawable/tab_press"?/>??
</selector>???
3.編寫indicator的布局文件(不妨也叫tab_indicator.xml),將其放入layout文件夾下,代碼如下:
?
<?xml?version="1.0"?encoding="utf-8"?>??
<RelativeLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
????android:layout_width="0dip"??
????android:layout_height="64dip"??
????android:layout_weight="1"??
????android:layout_marginLeft="-3dip"??
????android:layout_marginRight="-3dip"??
????android:orientation="vertical"??
????android:background="@drawable/tab_indicator">??
????<ImageView?android:id="@+id/icon"??
????????android:layout_width="wrap_content"??
????????android:layout_height="wrap_content"??
????????android:layout_centerHorizontal="true"??
????/>??
????<TextView?android:id="@+id/title"??
????????android:layout_width="wrap_content"??
????????android:layout_height="wrap_content"??
????????android:layout_alignParentBottom="true"??
????????android:layout_centerHorizontal="true"??
????????style="?android:attr/tabWidgetStyle"?mce_style="?android:attr/tabWidgetStyle"??
????/>???
4.接下來就是在TabActivity中使用我們自己編寫的Tab樣式了:
?
//?首先獲取TabWidget??
mTabHost?=?getTabHost();??
LinearLayout?ll?=?(LinearLayout)mTabHost.getChildAt(0);??
TabWidget?tw?=?(TabWidget)ll.getChildAt(0);???
然后用類似如下代碼創建TabSpec,就大功告成了。
?
RelativeLayout?tabIndicator1?=?(RelativeLayout)?LayoutInflater.from(this).inflate(R.layout.tab_indicator,?tw,?false);??
TextView?tvTab1?=?(TextView)tabIndicator1.getChildAt(1);??
tvTab1.setText("tab1");??
mTabHot?=?mTabHost.newTabSpec("TAB_1")??
????????.setIndicator(tabIndicator1)??
????????.setContent(contentIntent);??
tab_unselected.9.png?tab_selected.9.pngtab_press.9.pngtab_focus.9.png
這4個資源分別代表Tab的4種狀態。
2.定義Tab的selector樣式(就叫它tab_indicator.xml好了),將其放入drawable文件夾下,代碼如下:
?
<?xml?version="1.0"?encoding="utf-8"?>??
<selector?xmlns:android="http://schemas.android.com/apk/res/android">??
????<!--?Non?focused?states?-->??
????<item?android:state_focused="false"?android:state_selected="false"?android:state_pressed="false"?android:drawable="@drawable/tab_unselected"?/>??
????<item?android:state_focused="false"?android:state_selected="true"?android:state_pressed="false"?android:drawable="@drawable/tab_selected"?/>??
????<!--?Focused?states?-->??
????<item?android:state_focused="true"?android:state_selected="false"?android:state_pressed="false"?android:drawable="@drawable/tab_focus"?/>??
????<item?android:state_focused="true"?android:state_selected="true"?android:state_pressed="false"?android:drawable="@drawable/tab_focus"?/>??
????<!--?Pressed?-->??
????<item?android:state_pressed="true"?android:drawable="@drawable/tab_press"?/>??
</selector>???
3.編寫indicator的布局文件(不妨也叫tab_indicator.xml),將其放入layout文件夾下,代碼如下:
?
<?xml?version="1.0"?encoding="utf-8"?>??
<RelativeLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
????android:layout_width="0dip"??
????android:layout_height="64dip"??
????android:layout_weight="1"??
????android:layout_marginLeft="-3dip"??
????android:layout_marginRight="-3dip"??
????android:orientation="vertical"??
????android:background="@drawable/tab_indicator">??
????<ImageView?android:id="@+id/icon"??
????????android:layout_width="wrap_content"??
????????android:layout_height="wrap_content"??
????????android:layout_centerHorizontal="true"??
????/>??
????<TextView?android:id="@+id/title"??
????????android:layout_width="wrap_content"??
????????android:layout_height="wrap_content"??
????????android:layout_alignParentBottom="true"??
????????android:layout_centerHorizontal="true"??
????????style="?android:attr/tabWidgetStyle"?mce_style="?android:attr/tabWidgetStyle"??
????/>???
4.接下來就是在TabActivity中使用我們自己編寫的Tab樣式了:
?
//?首先獲取TabWidget??
mTabHost?=?getTabHost();??
LinearLayout?ll?=?(LinearLayout)mTabHost.getChildAt(0);??
TabWidget?tw?=?(TabWidget)ll.getChildAt(0);???
然后用類似如下代碼創建TabSpec,就大功告成了。
?
RelativeLayout?tabIndicator1?=?(RelativeLayout)?LayoutInflater.from(this).inflate(R.layout.tab_indicator,?tw,?false);??
TextView?tvTab1?=?(TextView)tabIndicator1.getChildAt(1);??
tvTab1.setText("tab1");??
mTabHot?=?mTabHost.newTabSpec("TAB_1")??
????????.setIndicator(tabIndicator1)??
????????.setContent(contentIntent);??
?
總結
以上是生活随笔為你收集整理的Android: 自定义Tab样式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JEECG 智能开发平台二次开发帮助文档
- 下一篇: 数据仓库组件:Hive环境搭建和基础用法