Fragment+FragmentTabHost組件(實現新浪微博底部欄)

FragmentTabhost 基於Fragement機制的輕量級框架組合,幫助你快速創建項目安卓開發複習筆記——Fragment+FragmentTabHost組件(實現新浪微

之前2篇文章的鏈接:

安卓開發複習筆記——TabHost組件(一)(實現底部菜單導航)

安卓開發複習筆記——TabHost組件(二)(實現底部菜單導航)

 

關於Fragment類在之前的安卓開發複習筆記——Fragment+ViewPager組件(高仿微信界面)也介紹過,這裏就不再重複闡述了。

國際慣例,先來張效果圖:

安卓開發複習筆記——Fragment+FragmentTabHost組件(實現新浪微

 

下面直接上代碼了,註釋很全,看過我前2篇文章的朋友,肯定秒懂的,哈哈~

 

activity_main.xml(主佈局文件)

安卓開發複習筆記——Fragment+FragmentTabHost組件(實現新浪微
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="vertical" >
 6 
 7     <!-- 存放主要頁面內容 -->
 8 
 9     <FrameLayout
10         android:id="@+id/maincontent"
11         android:layout_width="fill_parent"
12         android:layout_height="0dp"
13         android:layout_weight="1" >
14     </FrameLayout>
15     
16     <!-- 底層菜單 -->
17 
18     <android.support.v4.app.FragmentTabHost
19         android:id="@android:id/tabhost"
20         android:layout_width="fill_parent"
21         android:layout_height="wrap_content"
22         android:background="@drawable/maintab_toolbar_bg" >
23 
24         <FrameLayout
25             android:id="@android:id/tabcontent"
26             android:layout_width="0dp"
27             android:layout_height="0dp"
28             android:layout_weight="0" >
29         </FrameLayout>
30     </android.support.v4.app.FragmentTabHost>
31 
32 </LinearLayout>
安卓開發複習筆記——Fragment+FragmentTabHost組件(實現新浪微

 

fragment.xml(由於只有文字不同,這裏只給出一個)

安卓開發複習筆記——Fragment+FragmentTabHost組件(實現新浪微
 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5  >
 6 
 7 
 8     <TextView 
 9         android:id="@+id/text"
10         android:layout_width="wrap_content"
11         android:layout_height="wrap_content"
12         android:layout_centerInParent="true"
13         android:text="我是第一個Fragment"
14         android:textSize="20dp"
15         />
16 
17 
18 </RelativeLayout>
安卓開發複習筆記——Fragment+FragmentTabHost組件(實現新浪微

 

tabcontent.xml(具體底部菜單詳細布局)

安卓開發複習筆記——Fragment+FragmentTabHost組件(實現新浪微
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="wrap_content"
 4     android:layout_height="wrap_content"
 5     android:gravity="center_horizontal"
 6     android:orientation="vertical" >
 7 
 8     <ImageView 
 9         android:id="@+id/image"
10         android:layout_height="wrap_content"
11         android:layout_width="wrap_content"
12         />
13     <TextView 
14         android:id="@+id/text"
15         android:padding="2dp"
16         android:layout_width="wrap_content"
17         android:layout_height="wrap_content"
18         android:textColor="@android:color/white"
19         />
20 
21 
22 </LinearLayout>
安卓開發複習筆記——Fragment+FragmentTabHost組件(實現新浪微

 

bt_selector.xml(底部菜單點擊背景)

安卓開發複習筆記——Fragment+FragmentTabHost組件(實現新浪微
1 <?xml version="1.0" encoding="utf-8"?>
2 <selector xmlns:android="http://schemas.android.com/apk/res/android">
3 
4     <item android:drawable="@drawable/home_btn_bg" android:state_pressed="true"></item>
5     <item android:drawable="@drawable/home_btn_bg" android:state_selected="true"></item>
6 
7 </selector>
安卓開發複習筆記——Fragment+FragmentTabHost組件(實現新浪微

 

bt_home_selector.xml(底部菜單按鈕效果)

安卓開發複習筆記——Fragment+FragmentTabHost組件(實現新浪微
1 <?xml version="1.0" encoding="utf-8"?>
2 <selector xmlns:android="http://schemas.android.com/apk/res/android">
3 
4     <item android:drawable="@drawable/icon_home_sel" android:state_selected="true"></item>
5     <item android:drawable="@drawable/icon_home_nor"></item>
6 
7 </selector>
安卓開發複習筆記——Fragment+FragmentTabHost組件(實現新浪微

 

FragmentPage1-FragmentPage5.java

安卓開發複習筆記——Fragment+FragmentTabHost組件(實現新浪微
 1 package com.example.newtabhosttest;
 2 
 3 import android.os.Bundle;
 4 import android.support.annotation.Nullable;
 5 import android.support.v4.app.Fragment;
 6 import android.view.LayoutInflater;
 7 import android.view.View;
 8 import android.view.ViewGroup;
 9 
10 public class FragmentPage1 extends Fragment{
11         @Override
12         public View onCreateView(LayoutInflater inflater,
13                 @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
14             return inflater.inflate(R.layout.fragment1, null);
15         }
16 
17 }
安卓開發複習筆記——Fragment+FragmentTabHost組件(實現新浪微

 

MainActivity.java(主代碼)

安卓開發複習筆記——Fragment+FragmentTabHost組件(實現新浪微
 1 package com.example.newtabhosttest;
 2 
 3 import android.os.Bundle;
 4 import android.support.v4.app.FragmentActivity;
 5 import android.support.v4.app.FragmentTabHost;
 6 import android.view.View;
 7 import android.widget.ImageView;
 8 import android.widget.TabHost.TabSpec;
 9 import android.widget.TextView;
10 
11 public class MainActivity extends FragmentActivity {
12 
13     private FragmentTabHost fragmentTabHost;
14     private String texts[] = { "首頁", "消息", "好友", "廣場", "更多" };
15     private int imageButton[] = { R.drawable.bt_home_selector,
16             R.drawable.bt_message_selector, R.drawable.bt_selfinfo_selector,R.drawable.bt_square_selector ,R.drawable.bt_more_selector};
17      private Class fragmentArray[] = {FragmentPage1.class,FragmentPage2.class,FragmentPage3.class,FragmentPage4.class,FragmentPage5.class};  
18 
19     @Override
20     protected void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.activity_main);
23 
24         // 實例化tabhost
25         fragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
26         fragmentTabHost.setup(this, getSupportFragmentManager(),
27                 R.id.maincontent);
28 
29         for (int i = 0; i < texts.length; i++) {
30             TabSpec spec=fragmentTabHost.newTabSpec(texts[i]).setIndicator(getView(i));
31             
32             fragmentTabHost.addTab(spec, fragmentArray[i], null);
33             
34             //設置背景(必須在addTab之後,由於需要子節點(底部菜單按鈕)否則會出現空指針異常)
35             fragmentTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.bt_selector);
36         }
37 
38     }
39 
40     private View getView(int i) {
41         //取得佈局實例
42         View view=View.inflate(MainActivity.this, R.layout.tabcontent, null);
43         
44         //取得佈局對象
45         ImageView imageView=(ImageView) view.findViewById(R.id.image);
46         TextView textView=(TextView) view.findViewById(R.id.text);
47         
48         //設置圖標
49         imageView.setImageResource(imageButton[i]);
50         //設置標題
51         textView.setText(texts[i]);
52         return view;
53     }
54 
55 }
安卓開發複習筆記——Fragment+FragmentTabHost組件(實現新浪微

 

到這裏代碼就結束了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章