Android片段实现多个页面切换效果

  

现在的应用首页大部分屏幕的下方显示一行标签标签选项,点击不同的标签就可以切换到不同的界面。如下图:

  

 Android片段实现多个页面切换效果

  

我们之前都是用TabHost来实现,但是殊不知,TabHost并非是那么的简单,它的可扩展性非常的差,不能随意地定制标签项显示的内容,而且运行还要依赖于ActivityGroup.ActivityGroup原本主要是用于为每一个TabHost的子项管理一个单独的活动,但目前已经被废弃了。下面就借助片段来完成类似于TabHost一般的效果。

  

先实现主界面布局main_layout.xml:

        & lt; LinearLayout xmlns: android=" http://schemas.android.com/apk/res/android "   android: layout_width=" match_parent "   android: layout_height=" match_parent "   面向android:=按怪薄北?      & lt; FrameLayout   android: id=癅 + id/内容”   android: layout_width=" match_parent "   android: layout_height=" 0 dp”   android: layout_weight=?”比;   & lt;/FrameLayout>      & lt;视图   android: layout_width=" match_parent "   android: layout_height=" 0.5 dp "   android:背景=" # 000000 "/比;      LinearLayout & lt;   android: layout_width=" match_parent "   android: layout_height=" 60 dp "   android:背景=" # ffffff "   面向android:="水平"比;      & lt;使用   android: id=癅 + id/message_layout”   android: layout_width=" 0 dp”   android: layout_height=" match_parent "   android: layout_weight=?”比;      LinearLayout & lt;   android: layout_width=" match_parent "   android: layout_height=" wrap_content "   android: layout_centerVertical=" true "   面向android:=按怪薄北?      & lt; ImageView   android: id=癅 + id/message_image”   android: layout_width=" 20 dp”   android: layout_height=" 20 dp”   android: layout_gravity=" center_horizontal "   android: src=" https://www.yisu.com/zixun/@drawable ic_launcher "/比;      & lt; TextView   android: id=癅 + id/message_text”   android: layout_width=" wrap_content "   android: layout_height=" wrap_content "   android: layout_gravity=" center_horizontal "   android:文本="消息”   android:输入textColor=" # 82858 b”/比;   & lt;/LinearLayout>   & lt;/RelativeLayout>      & lt;视图   android: layout_width=" 0.5 dp "   android: layout_height=" match_parent "   android:背景=" # 000000 "/比;      & lt;使用   android: id=癅 + id/contacts_layout”   android: layout_width=" 0 dp”   android: layout_height=" match_parent "   android: layout_weight=?”比;      LinearLayout & lt;   android: layout_width=" match_parent "   android: layout_height=" wrap_content "   android: layout_centerVertical=" true "   面向android:=按怪薄北?      & lt; ImageView   android: id=癅 + id/contacts_image”   android: layout_width=" 20 dp”   android: layout_height=" 20 dp”   android: layout_gravity=" center_horizontal "   android: src=" https://www.yisu.com/zixun/@drawable ic_launcher "/比;      & lt; TextView   android: id=癅 + id/contacts_text”   android: layout_width=" wrap_content "   android: layout_height=" wrap_content "   android: layout_gravity=" center_horizontal "   android:文本="联系人”   android:输入textColor=" # 82858 b”/比;   & lt;/LinearLayout>   & lt;/RelativeLayout>      & lt;视图   android: layout_width=" 0.5 dp "   android: layout_height=" match_parent "   android:背景=" # 000000 "/比;      & lt;使用   android: id=癅 + id/news_layout”   android: layout_width=" 0 dp”   android: layout_height=" match_parent "   android: layout_weight=?”比;      LinearLayout & lt;   android: layout_width=" match_parent "   android: layout_height=" wrap_content "   android: layout_centerVertical=" true "   面向android:=按怪薄北?      & lt; ImageView   android: id=癅 + id/news_image”   android: layout_width=" 20 dp”   android: layout_height=" 20 dp”   android: layout_gravity=" center_horizontal "   android: src=" https://www.yisu.com/zixun/@drawable ic_launcher "/比;      & lt; TextView   android: id=癅 + id/news_text”   android: layout_width=" wrap_content "   android: layout_height=" wrap_content "   android: layout_gravity=" center_horizontal "   android:文本="动态”   android:输入textColor=" # 82858 b”/比;   & lt;/LinearLayout>   & lt;/RelativeLayout>      & lt;视图   android: layout_width=" 0.5 dp "   android: layout_height=" match_parent "   android:背景=" # 000000 "/比;      & lt;使用   android: id=癅 + id/setting_layout”   android: layout_width=" 0 dp”   android: layout_height=" match_parent "   android: layout_weight=?”比;      LinearLayout & lt;   android: layout_width=" match_parent "   android: layout_height=" wrap_content "   android: layout_centerVertical=" true "   面向android:=按怪薄北?      & lt; ImageView   android: id=癅 + id/setting_image”   android: layout_width=" 20 dp”   android: layout_height=" 20 dp”   android: layout_gravity=" center_horizontal "   android: src=" https://www.yisu.com/zixun/@drawable ic_launcher "/比;      & lt; TextView   android: id=癅 + id/setting_text”   android: layout_width=" wrap_content "   android: layout_height=" wrap_content "   android: layout_gravity=" center_horizontal "   android:文本="设置”   android:输入textColor=" # 82858 b”/比;   & lt;/LinearLayout>   & lt;/RelativeLayout>   & lt;/LinearLayout>      & lt;/LinearLayout>   

Android片段实现多个页面切换效果