【Android论文栏目提醒】:以下是网学会员为您推荐的Android论文-Android_标签 Android 标签/TabActivity 深度研究 - 编程语言,希望本篇文章对您学习有所帮助。
Android 标签/TabActivity 深度研究何谓标签 印象最深刻的应该是这个 现在 我们将通过一系列的扩展来研究之 1. 自定义 TabActivity 使得标签处于屏幕下方 2. 各个标签所用布局 既可在 .xml 中定义 也可在 .java 中定义 3. 更改标签布局 1. 标签页 在 屏幕下方一个典型的标签 Activity 是由 2 部分构成的 且其 id 都有规定 即: TabWidget 用于展示标签页 idtabs FrameLayout 用于展示隶属于各个标签的具体布局 idtabcontent 基本布局如下:Xml 代码 xml version1.0 encodingutf-8 TabHost xmlns:androidhttp://schemas.
android.com/apk/res/androidandroid:idandroid:id/tabhostandroid:layout_widthfill_parentandroid:layout_heightfill_parent LinearLayoutandroid:orientationverticalandroid:gravitybottomandroid:layout_widthfill_parentandroid:layout_heightfill_parent FrameLayoutandroid:idandroid:id/tabcontentandroid:layout_widthfill_parentandroid:layout_height200dip RelativeLayoutandroid:idid/view1
android:orientationverticalandroid:layout_widthfill_parentandroid:layout_heightfill_parent TextViewandroid:idid/textandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:textHello to Johnny.Griffinandroid:layout_centerInParenttrueandroid:textStylebolditalic / ImageViewandroid:layout_widthfill_parentandroid:layout_heightfill_parentandroid:srcdrawable/robotandroid:layout_toLeftOfid/text / /RelativeLayout TextViewandroid:idid/view2
android:layout_widthfill_parentandroid:layout_heightfill_parentandroid:text创新源于模仿 / TextViewandroid:idid/view3
android:layout_widthfill_parentandroid:layout_heightfill_parentandroid:text欢迎进入 droid 世界 / ImageViewandroid:idid/view4
android:layout_widthfill_parentandroid:layout_heightfill_parentandroid:srcdrawable/robot / /FrameLayout TabWidgetandroid:idandroid:id/tabsandroid:layout_widthfill_parentandroid:layout_heightwrap_content / /LinearLayout /TabHost xml version1.0 encodingutf-8 TabHost xmlns:androidhttp://schemas.
android.com/apk/res/androidandroid:idandroid:id/tabhostandroid:layout_widthfill_parentandroid:layout_heightfill_parent LinearLayoutandroid:orientationverticalandroid:gravitybottomandroid:layout_widthfill_parentandroid:layout_heightfill_parent FrameLayoutandroid:idandroid:id/tabcontentandroid:layout_widthfill_parentandroid:layout_height200dip RelativeLayoutandroid:idid/view1
android:orientationverticalandroid:layout_widthfill_parentandroid:layout_heightfill_parent TextViewandroid:idid/textandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:textHello to Johnny.Griffinandroid:layout_centerInParenttrueandroid:textStylebolditalic / ImageViewandroid:layout_widthfill_parentandroid:layout_heightfill_parentandroid:srcdrawable/robotandroid:layout_toLeftOfid/text / /RelativeLayout TextViewandroid:idid/view2
android:layout_widthfill_parentandroid:layout_heightfill_parentandroid:text创新源于模仿 / TextViewandroid:idid/view3
android:layout_widthfill_parentandroid:layout_heightfill_parentandroid:text欢迎进入 droid 世界 / pandroid:idid/view4
android:layout_widthfill_parentandroid:layout_heightfill_parentandroid:srcdrawable/robot / /FrameLayout TabWidgetandroid:idandroid:id/tabsandroid:layout_widthfill_parentandroid:layout_heightwrap_content / /LinearLayout /TabHost 得到 TabHost tHost 仅在 TabActivity 中有效Java 代码 tHost this.getTabHost tHost this.getTabHost 创建 4 个标签 并指定所使用的布局 Java 代码 public static final String Tab1 Tab1 public static final String Tab2 Tab2 public static final String Tab3 Tab3 public static final String Tab4 Tab4 public static final String Tab5 Tab5 tHost.addTabtHost.newTabSpecTab1.setIndicatorTab 1getResources.getDrawableR.drawable.icon.setContentR.id.view1 tHost.addTabtHost.newTabSpecTab2.setIndicatorTab 2getResources.getDrawableR.drawable.beijing_small.setContentR.id.view2 tHost.addTabtHost.newTabSpecTab3.setIndicatorTab 3.setContentR.id.view3 tHost.addTabtHost.newTabSpecTab4.setIndicatorTab 4.setContentR.id.view4 public static final String Tab1 Tab1 public static final String Tab2 Tab2 public static final String Tab3 Tab3 public static final String Tab4 Tab4 public static final String Tab5 Tab5 tHost.addTabtHost.newTabSpecTab1.setIndicatorTab 1getResources.getDrawableR.drawable.icon.setContentR.id.view1 tHost.addTabtHost.newTabSpecTab2.setIndicatorTab 2getResources.getDrawableR.drawable.beijing_small.setContentR.id.view2 tHost.addTabtHost.newTabSpecTab3.setIndicatorTab 3.setContentR.id.view3tHost.addTabtHost.newTabSpecTab4.setIndicatorTab 4.setContentR.id.view4 设定监听器 用于监听 标签间切换事件Java 代码tHost.setOnTabChangedListenernew OnTabChangeListenerOverridepublic void onTabChangedString tabId // TODO Auto-generated method stubtHost.setOnTabChangedListenernew OnTabChangeListenerOverridepublic void onTabChangedString tabId // TODO Auto-generated method stub emulator 运行情况:2. 在 .java 中定义标签所需布局Java 代码public class CustomLayout implements TabHost.TabContentFactory Activity activityLayoutInflater inflaterHelperLinearLayout layoutpublic CustomLayout Activity a activity ainflaterHelper a.getLayoutInflater / inheritDoc ///tag 标记各个标签 public View createTabContentString tag return addCustomViewtag public View addCustomViewString id layout new LinearLayoutactivity layout.setOrientationLinearLayout.VERTICAL ifid.equalsTab1 ImageView iv new ImageViewactivity iv.setImageResourceR.drawable.beijing_big layout.addViewiv new LinearLayout.LayoutParamsLinearLayout.LayoutParams.FILL_PARENTLinearLayout.LayoutParams.FILL_PARENT else ifid.equalsTab2 EditText edit new EditTextactivity layout.addViewedit new LinearLayout.LayoutParamsLinearLayout.LayoutParams.FILL_PARENTLinearLayout.LayoutParams.FILL_PARENT Button btn new Buttonactivity btn.setTextOK btn.setWidth100 layout.addViewbtn new LinearLayout.LayoutParamsLinearLayout.LayoutParams.FILL_PARENTLinearLayout.LayoutParams.FILL_PARENT RadioGroup rGroup new RadioGroupactivity rGroup.setOrientationLinearLayout.HORIZONTAL RadioButton radio1 new RadioButtonactivity radio1.setTextRadio A rGroup.addViewradio1 RadioButton radio2 new RadioButtonactivity radio2.setTextRadio B rGroup.addViewradio2 layout.addViewrGroup new LinearLayout.LayoutParamsLinearLayout.LayoutParams.WRAP_CONTENTLinearLayout.LayoutParams.WRAP_CONTENT else ifid.equalsTab3 LinearLayout.LayoutParams param3 new LinearLayout.LayoutParamsLinearLayout.LayoutParams.FILL_PARENTLinearLayout.LayoutParams.FILL_PARENT layout.addViewinflaterHelper.inflateR.layout.hello nullparam3 else ifid.equalsTab4 TextView tv new TextViewactivity tv.setTextHelloTags tv.setGravityGravity.CENTER layout.addViewtv return layout public class CustomLayout implements TabHost.TabContentFactory Activity activity LayoutInflater inflaterHelper LinearLayout layout public CustomLayout Activity a activity a inflaterHelper a.getLayoutInflater / inheritDoc ///tag 标记各个标签 public View createTabContentString tag return addCustomViewtag public View addCustomViewString id layout new LinearLayoutactivity layout.setOrientationLinearLayout.VERTICAL ifid.equalsTab1 ImageView iv new ImageViewactivity iv.setImageResourceR.drawable.beijing_big layout.addViewiv new LinearLayout.LayoutParamsLinearLayout.LayoutParams.FILL_PARENTLinearLayout.LayoutParams.FILL_PARENT else ifid.equalsTab2 EditText edit new EditTextactivity layout.addViewedit new LinearLayout.LayoutParamsLinearLayout.LayoutParams.FILL_PARENTLinearLayout.LayoutParams.FILL_PARENT Button btn new Buttonactivity btn.setTextOK btn.setWidth100 layout.addViewbtn new LinearLayout.LayoutParamsLinearLayout.LayoutParams.FILL_PARENTLinearLayout.LayoutParams.FILL_PARENT RadioGroup rGroup new RadioGroupactivity rGroup.setOrientationLinearLayout.HORIZONTAL RadioButton radio1 new RadioButtonactivity radio1.setTextRadio A rGroup.addViewradio1 RadioButton radio2 new RadioButtonactivity radio2.setTextRadio B rGroup.addViewradio2 layout.addViewrGroup new LinearLayout.LayoutParamsLinearLayout.LayoutParams.WRAP_CONTENTLinearLayout.LayoutParams.WRAP_CONTENT else ifid.equalsTab3 LinearLayout.LayoutParams param3 new LinearLayout.LayoutParamsLinearLayout.LayoutParams.FILL_PARENTLinearLayout.LayoutParams.FILL_PARENT layout.addViewinflaterHelper.inflateR.layout.hello nullparam3 else ifid.equalsTab4 TextView tv new TextViewactivity tv.setTextHelloTags tv.setGravityGravity.CENTER layout.addViewtv return layout 如何使用 Java 代码 CustomLayout ct new CustomLayoutthis tHost.addTabtHost.newTabSpecTab4.setIndicatorTab 4.setContentct CustomLayout ct new CustomLayoutthis tHost.addTabtHost.newTabSpecTab4.setIndicatorTab 4.setContentct emulator 运行结果: 3. 改变标签布局 可能很多人对 TabActivity 不满意 原因之一:其很不美观 而不美观的根源就是:标签的问题 其图像和文字相互覆盖 导致的 那么 我们可以自己扩展么 当然 TabWidget 理解: 1. TabWidget 为 horizontal 的 LinearLayout 2. 且 其包含的标签又是一个 RelativeLayout 3. 每个标签 RelativeLayout 里面包含 2 个 View: TextView ImageView 因此 我们甚至可以推算出其布局为: Java 代码 xml version1.0 encodingutf-8 LinearLayout xmlns:androidhttp://schemas.
android.com/apk/res/androidandroid:orientationhorizontalandroid:layout_widthfill_parentandroid:layout_heightfill_parent RelativeLayoutandroid:orientationverticalandroid:layout_widthfill_parentandroid:layout_heightwrap_content ImageView / TextView / /RelativeLayout RelativeLayoutandroid:orientationverticalandroid:layout_widthfill_parentandroid:layout_heightwrap_content ImageView / TextView / /RelativeLayout RelativeLayoutandroid:orientationverticalandroid:layout_widthfill_parentandroid:layout_heightwrap_content ImageView / TextView / /RelativeLayout RelativeLayoutandroid:orientationverticalandroid:layout_widthfill_parentandroid:layout_heightwrap_content ImageView / TextView / /RelativeLayout /LinearLayout xml version1.0 encodingutf-8 LinearLayout xmlns:androidhttp://schemas.
android.com/apk/res/androidandroid:orientationhorizontalandroid:layout_widthfill_parentandroid:layout_heightfill_parent RelativeLayoutandroid:orientationverticalandroid:layout_widthfill_parentandroid:layout_heightwrap_content ImageView / TextView / /RelativeLayout RelativeLayoutandroid:orientationverticalandroid:layout_widthfill_parentandroid:layout_heightwrap_content ImageView / TextView / /RelativeLayout RelativeLayoutandroid:orientationverticalandroid:layout_widthfill_parentandroid:layout_heightwrap_content ImageView / TextView / /RelativeLayout RelativeLayoutandroid:orientationverticalandroid:layout_widthfill_parentandroid:layout_heightwrap_content ImageView / TextView / /RelativeLayout /LinearLayout 去掉系统默认的布局 即 在 setIndicator 中置空 修改如下:Java 代码tHost.addTabtHost.newTabSpecTab1.setIndicator.setContentct tHost.addTabtHost.newTabSpecTab1.setIndicator.setContentct 写道 可能有人会说:那我不调用 setIndicator 不久可以了么 不行 否则 会报错 自己定义布局 并 指定显示的内容 Java 代码 public View composeLayoutString s int i LinearLayout layout new LinearLayoutthis layout.setOrientationLinearLayout.VERTICAL TextView tv new TextViewthis tv.setGravityGravity.CENTER tv.setSingleLinetrue tv.setTexts layout.addViewtv new LinearLayout.LayoutParamsLinearLayout.LayoutParams.FILL_PARENTLinearLayout.LayoutParams.WRAP_CONTENT ImageView iv new ImageViewthis iv.setImageResourcei layout.addViewiv new LinearLayout.LayoutParamsLinearLayout.LayoutParams.FILL_PARENTLinearLayout.LayoutParams.WRAP_CONTENT return layout public View composeLayoutString s int i LinearLayout layout new LinearLayoutthis layout.setOrientationLinearLayout.VERTICAL TextView tv new TextViewthis tv.setGravityGravity.CENTER tv.setSingleLinetrue tv.setTexts layout.addViewtv new LinearLayout.LayoutParamsLinearLayout.LayoutParams.FILL_PARENTLinearLayout.LayoutParams.WRAP_CONTENT ImageView iv new ImageViewthis iv.setImageResourcei layout.addViewiv new LinearLayout.LayoutParamsLinearLayout.LayoutParams.FILL_PARENTLinearLayout.LayoutParams.WRAP_CONTENT return layout 得到 TabWidget 实例 tw Java 代码 LinearLayout llLinearLayouttHost.getChildAt0 tw TabWidgetll.getChildAt1 LinearLayout llLinearLayouttHost.getChildAt0 tw TabWidgetll.getChildAt1 得到 TabWidget 内的具体某个 Layout 并使用上面的布局 composeLayout Java 代码 public void updateWidgetViewint iString textint image RelativeLayout rl RelativeLayouttw.getChildAti rl.addViewcomposeLayouttextimage