
1.TabActivity 继承自Activity其内部定义好了TabHost可以通过getTabHost()获取TabHost。TabHost 包含了两种子元素一些可以自由选择的Tab及 与这些tab对应的内容tabContent在layout的TabHost下它们分别对应 TabWidget (用于展示标签页 id固定为tabs )和 FrameLayout(用于展示隶属于各个标签的具体布局 id固定为tabcontent)。Tabhost默认的布局文件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 TabWidgetandroid:idandroid:id/tabsandroid:layout_widthfill_parentandroid:layout_heightwrap_content /FrameLayoutandroid:idandroid:id/tabcontentandroid:layout_widthfill_parentandroid:layout_height200dip /FrameLayout/LinearLayout/TabHost2.最简单的TabActivity示例不用设置setContentView(R.layout.main); 不依靠布局文件public class ILoveConactActivity extends TabActivity implements TabHost.TabContentFactory,OnTabChangeListener{public void onCreate(Bundle icicle){super.onCreate(icicle);//取得TabHost对象TabHost tabHostthis.getTabHost();tabHost.setOnTabChangedListener(this);for(int i0;i4;i){String tabItemTitletabi;String tabItemTagi;TabSpec tabSpectabHost.newTabSpec(tabItemTag);tabSpec.setIndicator(tabItemTitle);//或者setIndicator(CharSequence label, Drawable icon) setIndicator(View view)/*或者tabSpec.setContent(R.id.viewId);//R.id.viewId是在布局文件中的view的id。setIndicator(View view)setContent(TabContentFactory contentFactory)时会动态确定内容。setContent(Intent intent) Specify an intent to use to launch an activity as the tab content.*/tabSpec.setContent(this);tabHost.addTab(tabSpec);}tabHost.setCurrentTab(1);//或者setCurrentTabByTag(String tag)//加上下面这4行代码使TabWidget到底部TabWidget tabWidget this.getTabWidget(); //或者tabHost.getTabWidget();LinearLayout layout (LinearLayout) tabHost.getChildAt(0);layout.removeViewAt(0);layout.addView(tabWidget);//把tabWidget 从第一个位置移除再添加到下面来。}Overridepublic void onTabChanged(String tabId){// TODO Auto-generated method stub}Overridepublic View createTabContent(String tag){TextView viewnew TextView(this);view.setText(tab content tag);return view;//不能返回null否则会报错。}}intent必须是一个关联Activity的intent。android中文api (59) —— TabHost.TabSpec - 农民伯伯 - 博客园3.对TabWidget的理解(1)TabWidget 为 horizontal 的 LinearLayout(2)且 其包含的标签又是一个RelativeLayout(3) 每个标签RelativeLayout 里面包含2个ViewTextView和ImageView推算出其布局为?xml version1.0 encodingutf-8?LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:orientationhorizontalandroid:layout_widthfill_parentandroid:layout_heightfill_parentRelativeLayoutandroid:orientationverticalandroid:layout_widthfill_parentandroid:layout_heightwrap_contentImageView /TextView //RelativeLayoutRelativeLayoutandroid:orientationverticalandroid:layout_widthfill_parentandroid:layout_heightwrap_contentImageView /TextView //RelativeLayoutRelativeLayoutandroid:orientationverticalandroid:layout_widthfill_parentandroid:layout_heightwrap_contentImageView /TextView //RelativeLayoutRelativeLayoutandroid:orientationverticalandroid:layout_widthfill_parentandroid:layout_heightwrap_contentImageView /TextView //RelativeLayout/LinearLayout4.基于默认的Tabhost布局文件自定义布局文件?xml version1.0 encodingutf-8?LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:orientationverticalandroid:layout_widthfill_parentandroid:layout_heightfill_parent!-- TabHost必须包含一个 TabWidget和一个FrameLayout--TabHost android:idid/tabhostandroid:layout_widthfill_parentandroid:layout_heightwrap_contentLinearLayoutandroid:orientationverticalandroid:layout_widthfill_parentandroid:layout_heightfill_parent!-- TabWidget的id属性必须为 android:id/tabs--TabWidget android:idandroid:id/tabsandroid:orientationhorizontalandroid:layout_widthfill_parentandroid:layout_heightwrap_content/TabWidget!-- FrameLayout的id属性必须为 android:id/tabcontent--FrameLayout android:idandroid:id/tabcontentandroid:layout_widthfill_parentandroid:layout_heightfill_parentTextView android:idid/view1android:layout_widthfill_parentandroid:layout_heightfill_parent/TextView android:idid/view2android:layout_widthfill_parentandroid:layout_heightfill_parent/TextView android:idid/view3android:layout_widthfill_parentandroid:layout_heightfill_parent//FrameLayout/LinearLayout/TabHost/LinearLayout5.TabHost中的子Activity虽然是用Intent参数来启动但实际上并没有去启动一个全新的Activity。维护了几个子View6.ActivityGroup的问题它是用来做Tab切换用的不是用做栈导航的在一个ActivityGroup中连续开启同一个Activity第2个及之后和第1个在创建菜单的函数调用顺序上不同在一个ActivityGroup中连续开启同一个Activity非同一个Activity后面的创建菜单的函数不再调用了...另外也没有StartActivityForResult函数。ActivityGroup中可以指定界面中的各个部分内容来自不同的Activity这时这些Actvitiy只作为内容管理单元。NavHostFragmentNavHostFragment 是 Android Jetpack Navigation 组件里的一个特殊 Fragment用来当作导航的宿主容器负责展示当前应该显示的那个页面也就是导航图里的“目的地”。 核心作用容器角色它是 Activity 和各个 Fragment 之间的“展示窗口”导航图里定义的所有页面都要通过它来显示。自动管理它会自动帮你处理页面之间的切换、返回栈管理以及转场动画。绑定关系每个 NavHostFragment 都配有一个 NavControllerNavController 是实际执行导航操作的核心对象。