现在Fragment的应用真的是越来越广泛了,之前Android在3.0版本加入Fragment的时候,主要是为了解决Android Pad屏幕比较大,空间不能充分利用的问题,但现在即使只是在手机上,也有很多的场景可以运用到Fragment了,今天我们就来学习其中一个特别棒的应用技巧。很多手机应用都会有一个非常类似的功能,即屏幕的下方显示一行Tab标签选项,点击不同的标签就可以切换到不同的界面,如以下几个应用所示:

 Android使用Fragment实现TabHost效果    Android使用Fragment实现TabHost效果    Android使用Fragment实现TabHost效果

直接上实例:

新建一个项目,起名就叫FragmentDemo,这里我使用的是4.0的API。  

下面开始编程工作,这里我们首先需要去编写一个类似于QQ的主界面,当然只会去编写界面最下方的TabHost部分,而不会编写上面的内容界面部分,因为内容界面是应该写在Fragment的布局里的。

打开或新建activity_main.xml作为程序的主布局文件,在里面加入如下代码:

  1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2     android:layout_width="match_parent"
  3     android:layout_height="match_parent"
  4     android:orientation="vertical" >
  5 
  6     <FrameLayout
  7         android:id="@+id/content"
  8         android:layout_width="match_parent"
  9         android:layout_height="0dp"
 10         android:layout_weight="1" >
 11     </FrameLayout>
 12 
 13     <LinearLayout
 14         android:layout_width="match_parent"
 15         android:layout_height="60dp"
 16         android:background="@drawable/tab_bg" >
 17 
 18         <RelativeLayout
 19             android:id="@+id/message_layout"
 20             android:layout_width="0dp"
 21             android:layout_height="match_parent"
 22             android:layout_weight="1" >
 23 
 24             <LinearLayout
 25                 android:layout_width="match_parent"
 26                 android:layout_height="wrap_content"
 27                 android:layout_centerVertical="true"
 28                 android:orientation="vertical" >
 29 
 30                 <ImageView
 31                     android:id="@+id/message_image"
 32                     android:layout_width="wrap_content"
 33                     android:layout_height="wrap_content"
 34                     android:layout_gravity="center_horizontal"
 35                     android:src="@drawable/message_unselected" />
 36 
 37                 <TextView
 38                     android:id="@+id/message_text"
 39                     android:layout_width="wrap_content"
 40                     android:layout_height="wrap_content"
 41                     android:layout_gravity="center_horizontal"
 42                     android:text="消息"
 43                     android:textColor="#82858b" />
 44             </LinearLayout>
 45         </RelativeLayout>
 46 
 47         <RelativeLayout
 48             android:id="@+id/contacts_layout"
 49             android:layout_width="0dp"
 50             android:layout_height="match_parent"
 51             android:layout_weight="1" >
 52 
 53             <LinearLayout
 54                 android:layout_width="match_parent"
 55                 android:layout_height="wrap_content"
 56                 android:layout_centerVertical="true"
 57                 android:orientation="vertical" >
 58 
 59                 <ImageView
 60                     android:id="@+id/contacts_image"
 61                     android:layout_width="wrap_content"
 62                     android:layout_height="wrap_content"
 63                     android:layout_gravity="center_horizontal"
 64                     android:src="@drawable/contacts_unselected" />
 65 
 66                 <TextView
 67                     android:id="@+id/contacts_text"
 68                     android:layout_width="wrap_content"
 69                     android:layout_height="wrap_content"
 70                     android:layout_gravity="center_horizontal"
 71                     android:text="联系人"
 72                     android:textColor="#82858b" />
 73             </LinearLayout>
 74         </RelativeLayout>
 75 
 76         <RelativeLayout
 77             android:id="@+id/news_layout"
 78             android:layout_width="0dp"
 79             android:layout_height="match_parent"
 80             android:layout_weight="1" >
 81 
 82             <LinearLayout
 83                 android:layout_width="match_parent"

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2023-03-09
  • 2022-12-23
  • 2021-10-28
  • 2021-09-27
  • 2021-08-17
猜你喜欢
  • 2021-07-10
  • 2022-01-17
  • 2022-12-23
  • 2022-12-23
  • 2021-04-25
  • 2022-12-23
相关资源
相似解决方案