我之前做分頁都是用GridView和ActivityGroup實現的.因為覺得TabHost不好用,還有就是自己水平差的原因吧.如果幫的話,重寫view任何問題都可以解決,呵呵,下面請看實現過程,其實很簡單.
針對TabHost的運用,我就不多講解了,網上例子好多,或者你也可以下載我的Demo查看,不過先聲明,寫的不好.
程式碼片段:
public class MyActivity extends TabActivity {private TabWidget tabWidget;/** Called when the activity is first created. */public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);Resources res = getResources(); // Resource object to get Drawablesfinal TabHost tabHost = getTabHost(); // The activity TabHostTabHost.TabSpec spec; // Resusable TabSpec for each tabIntent intent; // Reusable Intent for each tab// Create an Intent to launch an Activity for the tab (to be reused)intent = new Intent().setClass(this, ArtistsActivity.class);// Initialize a TabSpec for each tab and add it to the TabHostspec = tabHost.newTabSpec("artists").setIndicator("Artists",res.getDrawable(R.drawable.ic_tab_artists)).setContent(intent);tabHost.addTab(spec);// Do the same for the other tabsintent = new Intent().setClass(this, AlbumsActivity.class);spec = tabHost.newTabSpec("albums").setIndicator("Albums",res.getDrawable(R.drawable.ic_tab_artists)).setContent(intent);tabHost.addTab(spec);intent = new Intent().setClass(this, SongsActivity.class);spec = tabHost.newTabSpec("songs").setIndicator("Songs",res.getDrawable(R.drawable.ic_tab_artists)).setContent(intent);tabHost.addTab(spec);tabHost.setCurrentTab(2);View v;tabWidget = tabHost.getTabWidget();for (int i = 0; i < tabWidget.getChildCount(); i++) {// 擷取tabview項v = tabWidget.getChildAt(i);// 設定tab背景顏色v.setBackgroundResource(android.R.color.white);// 擷取textview控制項,(預設為白色)TextView textView = (TextView) v.findViewById(android.R.id.title);textView.setTextColor(Color.BLACK);// 預設選項要處理if (tabHost.getCurrentTab() == i)v.setBackgroundResource(R.drawable.renren_sdk_pay_repair_btn);}// tabchanged的監聽tabHost.setOnTabChangedListener(new OnTabChangeListener() {// tabId顯示的是:newTabSpec裡面的值@Overridepublic void onTabChanged(String tabId) {// 首先把所有的view背景初始化了.for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {View v = tabHost.getTabWidget().getChildAt(i);// 設定tab背景顏色v.setBackgroundResource(android.R.color.white);// 選中的進行處理if (tabHost.getCurrentTab() == i) {v.setBackgroundResource(R.drawable.renren_sdk_pay_repair_btn);}}}});}}
其實就是那幾行加註釋部分,相信你看一下就明白.
解決Tab下面白線方法:
首先我們要擷取Tab的分頁view和textview控制項,因為textview預設字型是白色.我們要進行處理.擷取後進行相應處理(也就是該加背景的加背景,該改變字型顏色的改變字型顏色).最後我們要對TabHost進行監聽(OnTabChangedListener);也不是什麼監聽,。就是用他的點擊執行事件.
下面是視圖:
源碼下載