`
dengbaoleng
  • 浏览: 1132878 次
文章分类
社区版块
存档分类
最新评论

标签/TabActivity 深度研究

 
阅读更多

何谓标签 印象最深刻的应该是这个

现在 我们将通过一系列的扩展来研究之

写道
1. 自定义TabActivity 使得标签处于屏幕下方
2. 各个标签所用布局 既可在 *.xml 中定义 也可在 *.java 中定义
3. 更改标签布局

1. 标签页 在 屏幕下方

写道
一个典型的标签Activity 是由2 部分构成的 且其id都有规定 即:
* TabWidget 用于展示标签页 id=tabs
* FrameLayout 用于展示隶属于各个标签的具体布局 id=tabcontent

* 基本布局如下:

Xml代码 复制代码收藏代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <TabHostxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@android:id/tabhost"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <LinearLayout
  7. android:orientation="vertical"
  8. android:gravity="bottom"
  9. android:layout_width="fill_parent"
  10. android:layout_height="fill_parent">
  11. <FrameLayout
  12. android:id="@android:id/tabcontent"
  13. android:layout_width="fill_parent"
  14. android:layout_height="200dip">
  15. <RelativeLayout
  16. android:id="@+id/view1"
  17. android:orientation="vertical"
  18. android:layout_width="fill_parent"
  19. android:layout_height="fill_parent"
  20. >
  21. <TextView
  22. android:id="@+id/text"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:text="HellotoJohnny.Griffin!"
  26. android:layout_centerInParent="true"
  27. android:textStyle="bold|italic"/>
  28. <ImageView
  29. android:layout_width="fill_parent"
  30. android:layout_height="fill_parent"
  31. android:src="@drawable/robot"
  32. android:layout_toLeftOf="@id/text"/>
  33. </RelativeLayout>
  34. <TextView
  35. android:id="@+id/view2"
  36. android:layout_width="fill_parent"
  37. android:layout_height="fill_parent"
  38. android:text="创新源于模仿!"/>
  39. <TextView
  40. android:id="@+id/view3"
  41. android:layout_width="fill_parent"
  42. android:layout_height="fill_parent"
  43. android:text="欢迎进入droid世界!"/>
  44. <ImageView
  45. android:id="@+id/view4"
  46. android:layout_width="fill_parent"
  47. android:layout_height="fill_parent"
  48. android:src="@drawable/robot"/>
  49. </FrameLayout>
  50. <TabWidget
  51. android:id="@android:id/tabs"
  52. android:layout_width="fill_parent"
  53. android:layout_height="wrap_content"/>
  54. </LinearLayout>
  55. </TabHost>

* 得到TabHost tHost 仅在TabActivity中有效

Java代码 复制代码收藏代码
  1. tHost=this.getTabHost();

* 创建4个标签 并指定所使用的布局

Java代码 复制代码收藏代码
  1. publicstaticfinalStringTab1="Tab1";
  2. publicstaticfinalStringTab2="Tab2";
  3. publicstaticfinalStringTab3="Tab3";
  4. publicstaticfinalStringTab4="Tab4";
  5. publicstaticfinalStringTab5="Tab5";
  6. tHost.addTab(tHost.newTabSpec(Tab1).setIndicator("Tab1",getResources().getDrawable(R.drawable.icon)).setContent(R.id.view1));
  7. tHost.addTab(tHost.newTabSpec(Tab2).setIndicator("Tab2",getResources().getDrawable(R.drawable.beijing_small)).setContent(R.id.view2));
  8. tHost.addTab(tHost.newTabSpec(Tab3).setIndicator("Tab3").setContent(R.id.view3));
  9. tHost.addTab(tHost.newTabSpec(Tab4).setIndicator("Tab4").setContent(R.id.view4));

* 设定监听器 用于监听 标签间切换事件

Java代码 复制代码收藏代码
  1. tHost.setOnTabChangedListener(newOnTabChangeListener(){
  2. @Override
  3. publicvoidonTabChanged(StringtabId){
  4. //TODOAuto-generatedmethodstub
  5. }
  6. });

* emulator 运行情况:

2. 在 *.java 中定义标签所需布局

Java代码 复制代码收藏代码
  1. publicclassCustomLayoutimplementsTabHost.TabContentFactory{
  2. Activityactivity;
  3. LayoutInflaterinflaterHelper;
  4. LinearLayoutlayout;
  5. publicCustomLayout(Activitya){
  6. activity=a;
  7. inflaterHelper=a.getLayoutInflater();
  8. }
  9. /**{@inheritDoc}*///tag标记各个标签
  10. publicViewcreateTabContent(Stringtag){
  11. returnaddCustomView(tag);
  12. }
  13. publicViewaddCustomView(Stringid){
  14. layout=newLinearLayout(activity);
  15. layout.setOrientation(LinearLayout.VERTICAL);
  16. if(id.equals(Tab1)){
  17. ImageViewiv=newImageView(activity);
  18. iv.setImageResource(R.drawable.beijing_big);
  19. layout.addView(iv,
  20. newLinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT));
  21. }
  22. elseif(id.equals(Tab2)){
  23. EditTextedit=newEditText(activity);
  24. layout.addView(edit,
  25. newLinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT));
  26. Buttonbtn=newButton(activity);
  27. btn.setText("OK");
  28. btn.setWidth(100);
  29. layout.addView(btn,
  30. newLinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT));
  31. RadioGrouprGroup=newRadioGroup(activity);
  32. rGroup.setOrientation(LinearLayout.HORIZONTAL);
  33. RadioButtonradio1=newRadioButton(activity);
  34. radio1.setText("RadioA");
  35. rGroup.addView(radio1);
  36. RadioButtonradio2=newRadioButton(activity);
  37. radio2.setText("RadioB");
  38. rGroup.addView(radio2);
  39. layout.addView(rGroup,
  40. newLinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
  41. }
  42. elseif(id.equals(Tab3)){
  43. LinearLayout.LayoutParamsparam3=
  44. newLinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
  45. layout.addView(inflaterHelper.inflate(R.layout.hello,null),param3);
  46. }
  47. elseif(id.equals(Tab4)){
  48. TextViewtv=newTextView(activity);
  49. tv.setText("HelloTags!");
  50. tv.setGravity(Gravity.CENTER);
  51. layout.addView(tv);
  52. }
  53. returnlayout;
  54. }
  55. }

* 如何使用:

Java代码 复制代码收藏代码
  1. CustomLayoutct=newCustomLayout(this);
  2. tHost.addTab(tHost.newTabSpec(Tab4).setIndicator("Tab4").setContent(ct));

* emulator 运行结果:

3. 改变标签布局

写道
可能很多人对TabActivity 不满意 原因之一:其很不美观 而不美观的根源就是:标签的问题 其图像和文字相互覆盖 导致的


那么 我们可以自己扩展么? 当然

写道
TabWidget 理解:

1. TabWidget 为 horizontal 的 LinearLayout
2. 且 其包含的标签又是一个RelativeLayout
3. 每个标签RelativeLayout 里面包含2个View: TextView ImageView

因此 我们甚至可以推算出其布局为:

Java代码 复制代码收藏代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="horizontal"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <RelativeLayout
  8. android:orientation="vertical"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content">
  11. <ImageView/>
  12. <TextView/>
  13. </RelativeLayout>
  14. <RelativeLayout
  15. android:orientation="vertical"
  16. android:layout_width="fill_parent"
  17. android:layout_height="wrap_content">
  18. <ImageView/>
  19. <TextView/>
  20. </RelativeLayout>
  21. <RelativeLayout
  22. android:orientation="vertical"
  23. android:layout_width="fill_parent"
  24. android:layout_height="wrap_content">
  25. <ImageView/>
  26. <TextView/>
  27. </RelativeLayout>
  28. <RelativeLayout
  29. android:orientation="vertical"
  30. android:layout_width="fill_parent"
  31. android:layout_height="wrap_content">
  32. <ImageView/>
  33. <TextView/>
  34. </RelativeLayout>
  35. </LinearLayout>

* 去掉系统默认的布局 即 在 setIndicator() 中置空 修改如下:

Java代码 复制代码收藏代码
  1. tHost.addTab(tHost.newTabSpec(Tab1).setIndicator("").setContent(ct));

写道
可能有人会说:那我不调用setIndicator() 不久可以了么 不行 否则 会报错

* 自己定义布局 并 指定显示的内容

Java代码 复制代码收藏代码
  1. publicViewcomposeLayout(Strings,inti){
  2. LinearLayoutlayout=newLinearLayout(this);
  3. layout.setOrientation(LinearLayout.VERTICAL);
  4. TextViewtv=newTextView(this);
  5. tv.setGravity(Gravity.CENTER);
  6. tv.setSingleLine(true);
  7. tv.setText(s);
  8. layout.addView(tv,
  9. newLinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
  10. ImageViewiv=newImageView(this);
  11. iv.setImageResource(i);
  12. layout.addView(iv,
  13. newLinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
  14. returnlayout;
  15. }

* 得到 TabWidget 实例 tw

Java代码 复制代码收藏代码
  1. LinearLayoutll=(LinearLayout)tHost.getChildAt(0);
  2. tw=(TabWidget)ll.getChildAt(1);

* 得到 TabWidget 内的具体某个Layout 并使用上面的布局 composeLayout()

Java代码 复制代码收藏代码
  1. publicvoidupdateWidgetView(inti,Stringtext,intimage){
  2. RelativeLayoutrl=(RelativeLayout)tw.getChildAt(i);
  3. rl.addView(composeLayout(text,image));
  4. }

* emulator 运行截图 // 前面 3个是使用新布局 最后一个是使用TabActivity 默认的布局 哪个好看 大家自己选择之

that's all!

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics