5. android:paddingLeft=\ 6. android:paddingRight=\
7. android:background=\> 8.
9. 16. android:background=\/> 17. 第三步:创建一个selector在drawable里面 命名tabmini.xml,用来点击TabHost的一个tab时TextView的变化: [html] view plaincopyprint? 1. 2. 3. xmlns:android=\> 4. 5. android:drawable=\/> 6. 7. android:drawable=\/> 8. 第四步:在Activity里实现TabHost: [java] view plaincopyprint? 1. package cn.li.tabstyle; 2. 3. import android.app.Activity; 4. import android.os.Bundle; 5. import android.view.LayoutInflater; 6. import android.view.View; 7. import android.widget.TabHost; 8. import android.widget.TextView; 9. 10. public class TabHostStyleActivity extends Activity { 11. /** Called when the activity is first created. */ 12. @Override 13. public void onCreate(Bundle savedInstanceState) { 14. super.onCreate(savedInstanceState); 15. setContentView(R.layout.main); 16. 17. View niTab = (View) LayoutInflater.from(this).inflate(R.layout.tabmini, null ); 18. TextView text0 = (TextView) niTab.findViewById(R.id.tab_label); 19. text0.setText(\20. 21. View woTab = (View) LayoutInflater.from(this).inflate(R.layout.tabmini, nu ll); 22. TextView text1 = (TextView) woTab.findViewById(R.id.tab_label); 23. text1.setText(\24. 25. View taTab = (View) LayoutInflater.from(this).inflate(R.layout.tabmini, null ); 26. TextView text2 = (TextView) taTab.findViewById(R.id.tab_label); 27. text2.setText(\28. 29. View weTab = (View) LayoutInflater.from(this).inflate(R.layout.tabmini, nu ll); 30. TextView text3 = (TextView) weTab.findViewById(R.id.tab_label); 31. text3.setText(\32. 33. TabHost tabHost = (TabHost)findViewById(R.id.tabhost); 34. tabHost.setup(); //Call setup() before adding tabs if loading TabHost usi ng findViewById(). 35. 36. tabHost.addTab(tabHost.newTabSpec(\ ntent(R.id.tab1)); 37. tabHost.addTab(tabHost.newTabSpec(\ Content(R.id.tab2)); 38. tabHost.addTab(tabHost.newTabSpec(\ ntent(R.id.tab3)); 39. tabHost.addTab(tabHost.newTabSpec(\ Content(R.id.tab4)); 40. } 41. } 这里我们用findViewById创建了TabHost,这样的话我们就需要在添加tab时调用TabHost的setup()方法;这里我们添加内容时添加的是布局,我们完全可以换成自己创建的Activity。 好了,让我们来看看运行效果吧: 好了,我们自定义的TabHost算是结束了。不过看到Activity里的代码很多都是重复的我们可以这样把他们简化: [java] view plaincopyprint? 1. package cn.li.tabstyle; 2. 3. import android.app.Activity; 4. import android.os.Bundle; 5. import android.view.LayoutInflater; 6. import android.view.View; 7. import android.widget.TabHost; 8. import android.widget.TextView; 9. 10. public class TabHostStyleActivity extends Activity { 11. /** Called when the activity is first created. */ 12. String[] title = new String[]{\ 13. View userTab,articeTab,feedTab,weTab; 14. View[] tabs = new View[]{userTab,articeTab,feedTab,weTab}; 15. int[] tabIds = new int[]{R.id.tab1,R.id.tab2,R.id.tab3,R.id.tab4}; 16. @Override 17. public void onCreate(Bundle savedInstanceState) { 18. super.onCreate(savedInstanceState); 19. setContentView(R.layout.main); 20. 21. TabHost tabHost = (TabHost)findViewById(R.id.tabhost); 22. tabHost.setup(); //Call setup() before adding tabs if loading TabHost usi ng findViewById(). 23. 24. for(int i=0;i 25. tabs[i] = (View) LayoutInflater.from(this).inflate(R.layout.tabmini, null); 26. TextView text = (TextView) tabs[i].findViewById(R.id.tab_label); 27. text.setText(title[i]); 28. tabHost.addTab(tabHost.newTabSpec(title[i]).setIndicator(tabs[i]).setC ontent(tabIds[i])); 29. } 30. } 31. } 百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库Android TabHost用法(3)在线全文阅读。
相关推荐: