77范文网 - 专业文章范例文档资料分享平台

Android TabHost用法(3)

来源:网络收集 时间:2019-01-07 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

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)在线全文阅读。

Android TabHost用法(3).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/411683.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: