App底部导航(Fragment与ActivityGroup)

本文介绍如何使用Android的Fragment来实现类似TabHost的功能。通过详细布局文件示例,展示了四个不同功能区的消息、联系人、动态及设置是如何被组织在一个主活动中的。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    Android的3.0版本加入Fragment。在API 13的时候Android就已经将ActivityGroup废弃掉了,并且官方推荐的替代方式就是使用Fragment,因为它使用起来更加的灵活。那么剩下的问题就是如何借助Fragment来完成类似于TabHost一般的效果了,因此我们自然要动起手来了。

》MainActivity.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical" >  
  
    <FrameLayout  
        android:id="@+id/content"  
        android:layout_width="match_parent"  
        android:layout_height="0dp"  
        android:layout_weight="1" >  
    </FrameLayout>  
  
    <LinearLayout  
        android:layout_width="match_parent"  
        android:layout_height="60dp"  
        android:background="@drawable/tab_bg" >  
  
        <RelativeLayout  
            android:id="@+id/message_layout"  
            android:layout_width="0dp"  
            android:layout_height="match_parent"  
            android:layout_weight="1" >  
  
            <LinearLayout  
                android:layout_width="match_parent"  
                android:layout_height="wrap_content"  
                android:layout_centerVertical="true"  
                android:orientation="vertical" >  
  
                <ImageView  
                    android:id="@+id/message_image"  
                    android:layout_width="wrap_content"  
                    android:layout_height="wrap_content"  
                    android:layout_gravity="center_horizontal"  
                    android:src="@drawable/message_unselected" />  
  
                <TextView  
                    android:id="@+id/message_text"  
                    android:layout_width="wrap_content"  
                    android:layout_height="wrap_content"  
                    android:layout_gravity="center_horizontal"  
                    android:text="消息"  
                    android:textColor="#82858b" />  
            </LinearLayout>  
        </RelativeLayout>  
  
        <RelativeLayout  
            android:id="@+id/contacts_layout"  
            android:layout_width="0dp"  
            android:layout_height="match_parent"  
            android:layout_weight="1" >  
  
            <LinearLayout  
                android:layout_width="match_parent"  
                android:layout_height="wrap_content"  
                android:layout_centerVertical="true"  
                android:orientation="vertical" >  
  
                <ImageView  
                    android:id="@+id/contacts_image"  
                    android:layout_width="wrap_content"  
                    android:layout_height="wrap_content"  
                    android:layout_gravity="center_horizontal"  
                    android:src="@drawable/contacts_unselected" />  
  
                <TextView  
                    android:id="@+id/contacts_text"  
                    android:layout_width="wrap_content"  
                    android:layout_height="wrap_content"  
                    android:layout_gravity="center_horizontal"  
                    android:text="联系人"  
                    android:textColor="#82858b" />  
            </LinearLayout>  
        </RelativeLayout>  
  
        <RelativeLayout  
            android:id="@+id/news_layout"  
            android:layout_width="0dp"  
            android:layout_height="match_parent"  
            android:layout_weight="1" >  
  
            <LinearLayout  
                android:layout_width="match_parent"  
                android:layout_height="wrap_content"  
                android:layout_centerVertical="true"  
                android:orientation="vertical" >  
  
                <ImageView  
                    android:id="@+id/news_image"  
                    android:layout_width="wrap_content"  
                    android:layout_height="wrap_content"  
                    android:layout_gravity="center_horizontal"  
                    android:src="@drawable/news_unselected" />  
  
                <TextView  
                    android:id="@+id/news_text"  
                    android:layout_width="wrap_content"  
                    android:layout_height="wrap_content"  
                    android:layout_gravity="center_horizontal"  
                    android:text="动态"  
                    android:textColor="#82858b" />  
            </LinearLayout>  
        </RelativeLayout>  
  
        <RelativeLayout  
            android:id="@+id/setting_layout"  
            android:layout_width="0dp"  
            android:layout_height="match_parent"  
            android:layout_weight="1" >  
  
            <LinearLayout  
                android:layout_width="match_parent"  
                android:layout_height="wrap_content"  
                android:layout_centerVertical="true"  
                android:orientation="vertical" >  
  
                <ImageView  
                    android:id="@+id/setting_image"  
                    android:layout_width="wrap_content"  
                    android:layout_height="wrap_content"  
                    android:layout_gravity="center_horizontal"  
                    android:src="@drawable/setting_unselected" />  
  
                <TextView  
                    android:id="@+id/setting_text"  
                    android:layout_width="wrap_content"  
                    android:layout_height="wrap_content"  
                    android:layout_gravity="center_horizontal"  
                    android:text="设置"  
                    android:textColor="#82858b" />  
            </LinearLayout>  
        </RelativeLayout>  
    </LinearLayout>  
  
</LinearLayout>  


转载地址:http://www.cnblogs.com/android100/p/Android-Fragment-ActivityGroup.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值