【Android UI設計與開發】第09期:底部菜單欄(四)Fragment+PopupWindow仿QQ空間最新版底部菜單欄

轉載出處:http://blog.csdn.net/yangyu20121224/article/details/9023451       

轉載,請自覺註明!

  


     在今天的這篇文章當中,我依然會以實戰加理論結合的方式教大家如何設計出自己覺得很炫的UI界面。好的,話不多說,進入正題。今天的這篇文章主要是以仿QQ空間的底部菜單欄效果爲主,實現的效果有:

<1>實現了點擊按鈕時的切換圖片效果;

<2>實現了點擊按鈕時的切換界面效果;

<3>實現了點擊中間圓形按鈕時彈出菜單以及按鈕圖片切換效果;

<4>實現了點擊空白處和返回鍵按鈕來關閉彈出菜單。


          有個地方需要注意的是,彈出菜單欄後,點擊裏面的選項按鈕會導致中間的圓形按鈕切換爲普通狀態,這是因爲在實際的項目中,點擊菜單選項按鈕之後會進入別的界面,所以也就不存在點擊了之後圓形的按鈕切換爲普通的狀態效果了,所以這裏也不需要太在意。爲了實現效果,這裏只適配了480x800 hdpi的屏幕大小。



一、效果圖

 

有圖纔有真相







二、項目結構圖




三、代碼詳細編寫


1、主佈局界面,activity_main.xml:

[html] view plaincopy
  1. <span style="font-size:12px;"><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.     <FrameLayout  
  7.         android:id="@+id/frame_content"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_above="@+id/frameLayout1"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_alignParentRight="true"  
  13.         android:layout_alignParentTop="true"  
  14.         android:background="#ffffff" >  
  15.     </FrameLayout>  
  16.   
  17.     <FrameLayout  
  18.         android:id="@+id/frameLayout1"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_alignParentBottom="true"  
  22.         android:layout_alignParentLeft="true"  
  23.         android:background="@drawable/toolbar_bg_normal" >  
  24.   
  25.         <LinearLayout  
  26.             android:layout_width="fill_parent"  
  27.             android:layout_height="fill_parent"  
  28.             android:layout_marginTop="1dp"  
  29.             android:gravity="center_horizontal" >  
  30.   
  31.             <FrameLayout  
  32.                 android:id="@+id/layout_friendfeed"  
  33.                 android:layout_width="fill_parent"  
  34.                 android:layout_height="48dp"  
  35.                 android:layout_weight="1"   
  36.                 android:background="@drawable/tab_btn_background">  
  37.   
  38.                 <ImageView  
  39.                     android:id="@+id/image_friendfeed"  
  40.                     android:layout_width="wrap_content"  
  41.                     android:layout_height="wrap_content"  
  42.                     android:layout_gravity="top|center"  
  43.                     android:layout_marginTop="1.0dip"  
  44.                     android:src="@drawable/tab_friendfeed_btn" />  
  45.   
  46.                 <TextView  
  47.                     android:layout_width="wrap_content"  
  48.                     android:layout_height="wrap_content"  
  49.                     android:layout_gravity="bottom|center"  
  50.                     android:layout_marginBottom="6.0dip"  
  51.                     android:text="動態"  
  52.                     android:textColor="#ffffff"  
  53.                     android:textSize="10sp" />  
  54.             </FrameLayout>  
  55.   
  56.             <FrameLayout  
  57.                 android:id="@+id/layout_myfeed"  
  58.                 android:layout_width="fill_parent"  
  59.                 android:layout_height="48dp"  
  60.                 android:layout_weight="1"   
  61.                 android:background="@drawable/tab_btn_background">  
  62.   
  63.                 <ImageView  
  64.                     android:id="@+id/image_myfeed"  
  65.                     android:layout_width="wrap_content"  
  66.                     android:layout_height="wrap_content"  
  67.                     android:layout_gravity="top|center"  
  68.                     android:layout_marginTop="1.0dip"  
  69.                     android:src="@drawable/tab_myfeed_btn" />  
  70.   
  71.                 <TextView  
  72.                     android:layout_width="wrap_content"  
  73.                     android:layout_height="wrap_content"  
  74.                     android:layout_gravity="bottom|center"  
  75.                     android:layout_marginBottom="6.0dip"  
  76.                     android:text="與我想關"  
  77.                     android:textColor="#ffffff"  
  78.                     android:textSize="10sp" />  
  79.             </FrameLayout>  
  80.   
  81.             <FrameLayout  
  82.                 android:layout_width="fill_parent"  
  83.                 android:layout_height="wrap_content"  
  84.                 android:layout_weight="1" >  
  85.             </FrameLayout>  
  86.   
  87.             <FrameLayout  
  88.                 android:id="@+id/layout_home"  
  89.                 android:layout_width="fill_parent"  
  90.                 android:layout_height="48dp"  
  91.                 android:layout_weight="1"   
  92.                 android:background="@drawable/tab_btn_background">  
  93.   
  94.                 <ImageView  
  95.                     android:id="@+id/image_home"  
  96.                     android:layout_width="wrap_content"  
  97.                     android:layout_height="wrap_content"  
  98.                     android:layout_gravity="top|center"  
  99.                     android:layout_marginTop="1.0dip"  
  100.                     android:src="@drawable/tab_home_btn" />  
  101.   
  102.                 <TextView  
  103.                     android:layout_width="wrap_content"  
  104.                     android:layout_height="wrap_content"  
  105.                     android:layout_gravity="bottom|center"  
  106.                     android:layout_marginBottom="6.0dip"  
  107.                     android:text="我的空間"  
  108.                     android:textColor="#ffffff"  
  109.                     android:textSize="10sp" />  
  110.             </FrameLayout>  
  111.   
  112.             <FrameLayout  
  113.                 android:id="@+id/layout_more"  
  114.                 android:layout_width="fill_parent"  
  115.                 android:layout_height="48dp"  
  116.                 android:layout_weight="1"   
  117.                 android:background="@drawable/tab_btn_background">  
  118.   
  119.                 <ImageView  
  120.                     android:id="@+id/image_more"  
  121.                     android:layout_width="wrap_content"  
  122.                     android:layout_height="wrap_content"  
  123.                     android:layout_gravity="top|center"  
  124.                     android:layout_marginTop="1.0dip"  
  125.                     android:src="@drawable/tab_more_btn" />  
  126.   
  127.                 <TextView  
  128.                     android:layout_width="wrap_content"  
  129.                     android:layout_height="wrap_content"  
  130.                     android:layout_gravity="bottom|center"  
  131.                     android:layout_marginBottom="6.0dip"  
  132.                     android:text="更多"  
  133.                     android:textColor="#ffffff"  
  134.                     android:textSize="10sp" />  
  135.             </FrameLayout>  
  136.         </LinearLayout>  
  137.     </FrameLayout>  
  138.   
  139.     <ImageView  
  140.         android:id="@+id/toggle_btn"  
  141.         android:layout_width="wrap_content"  
  142.         android:layout_height="wrap_content"  
  143.         android:layout_alignParentBottom="true"  
  144.         android:layout_centerHorizontal="true"  
  145.         android:src="@drawable/toolbar_btn_normal" />  
  146.   
  147.     <ImageView  
  148.         android:id="@+id/plus_btn"  
  149.         android:layout_width="wrap_content"  
  150.         android:layout_height="wrap_content"  
  151.         android:layout_alignTop="@+id/frameLayout1"  
  152.         android:layout_centerHorizontal="true"  
  153.         android:layout_marginTop="6dip"  
  154.         android:src="@drawable/toolbar_plus" />  
  155.   
  156. </RelativeLayout></span>  
         我覺得這個佈局界面是整個項目當中實現起來最複雜的地方,但是把思路理清楚了之後又覺得其實也沒有那麼複雜,詳細說一下我實現的步驟:

       <1> 最外層使用的是RelativeLayout,主要是爲了容易擺放底部菜單欄的位置;

       <2> 然後是一個FrameLayout,主要用來存放顯示Fragment的內容,這裏的ID取名爲frame_content是用來替換Fragment對象的,在後面的代碼中會用到;

       <3> 最下面的底部菜單欄中外層使用了FrameLayout,之所以使用FrameLayout是爲了讓底部菜單欄中間的按鈕也可以擺放進來,實現一種疊加的效果;

       <4> 裏面嵌套了LinearLayout,使用它是爲了能夠使用layout_weight屬性,可以用來更好的擺放按鈕,還可以實現自適應屏幕的效果(關於自適應屏幕的內容後面會有專題詳細講解)

       <5> 最後裏面又嵌套了一個FrameLayout,使用它可以很方便的實現圖標在上文字在下的效果,最主要的原因是使用它可以很容易的再添加一個消息提醒的小圖片(實際的開發中會用到,此項目中沒有用到)


2、彈出菜單的佈局界面,popwindow_layout.xml:

[html] view plaincopy
  1. <span style="font-size:12px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent" >  
  6.   
  7.     <LinearLayout  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_alignParentBottom="true"  
  11.         android:background="@drawable/popwindow_bg"  
  12.         android:orientation="vertical"  
  13.         tools:ignore="UselessParent" >  
  14.   
  15.         <LinearLayout  
  16.             android:layout_width="wrap_content"  
  17.             android:layout_height="wrap_content"  
  18.             android:layout_gravity="center_horizontal"  
  19.             android:layout_marginTop="15dp"  
  20.             android:orientation="horizontal" >  
  21.   
  22.             <LinearLayout  
  23.                 android:layout_width="wrap_content"  
  24.                 android:layout_height="wrap_content"  
  25.                 android:layout_margin="5dp"  
  26.                 android:layout_weight="1"  
  27.                 android:orientation="vertical" >  
  28.   
  29.                 <ImageView  
  30.                     android:layout_width="wrap_content"  
  31.                     android:layout_height="wrap_content"  
  32.                     android:layout_gravity="center_horizontal"  
  33.                     android:layout_marginTop="1.0dip"  
  34.                     android:src="@drawable/popwindow_write_btn" />  
  35.   
  36.                 <TextView  
  37.                     android:layout_width="wrap_content"  
  38.                     android:layout_height="wrap_content"  
  39.                     android:layout_gravity="center_horizontal"  
  40.                     android:layout_marginTop="5.0dip"  
  41.                     android:shadowColor="#ff000000"  
  42.                     android:shadowDx="1.0"  
  43.                     android:shadowDy="1.0"  
  44.                     android:shadowRadius="1.0"  
  45.                     android:text="說說"  
  46.                     android:textColor="#ffffffff"  
  47.                     android:textSize="13.0dip" />  
  48.             </LinearLayout>  
  49.   
  50.             <LinearLayout  
  51.                 android:layout_width="wrap_content"  
  52.                 android:layout_height="wrap_content"  
  53.                 android:layout_margin="5dp"  
  54.                 android:layout_weight="1"  
  55.                 android:orientation="vertical" >  
  56.   
  57.                 <ImageView  
  58.                     android:layout_width="wrap_content"  
  59.                     android:layout_height="wrap_content"  
  60.                     android:layout_gravity="center_horizontal"  
  61.                     android:layout_marginTop="1.0dip"  
  62.                     android:src="@drawable/popwindow_voice_btn" />  
  63.   
  64.                 <TextView  
  65.                     android:layout_width="wrap_content"  
  66.                     android:layout_height="wrap_content"  
  67.                     android:layout_gravity="center_horizontal"  
  68.                     android:layout_marginTop="5.0dip"  
  69.                     android:shadowColor="#ff000000"  
  70.                     android:shadowDx="1.0"  
  71.                     android:shadowDy="1.0"  
  72.                     android:shadowRadius="1.0"  
  73.                     android:text="語音"  
  74.                     android:textColor="#ffffffff"  
  75.                     android:textSize="13.0dip" />  
  76.             </LinearLayout>  
  77.   
  78.             <LinearLayout  
  79.                 android:layout_width="wrap_content"  
  80.                 android:layout_height="wrap_content"  
  81.                 android:layout_margin="5dp"  
  82.                 android:layout_weight="1"  
  83.                 android:orientation="vertical" >  
  84.   
  85.                 <ImageView  
  86.                     android:layout_width="wrap_content"  
  87.                     android:layout_height="wrap_content"  
  88.                     android:layout_gravity="center_horizontal"  
  89.                     android:layout_marginTop="1.0dip"  
  90.                     android:src="@drawable/popwindow_camera_btn" />  
  91.   
  92.                 <TextView  
  93.                     android:layout_width="wrap_content"  
  94.                     android:layout_height="wrap_content"  
  95.                     android:layout_gravity="center_horizontal"  
  96.                     android:layout_marginTop="5.0dip"  
  97.                     android:shadowColor="#ff000000"  
  98.                     android:shadowDx="1.0"  
  99.                     android:shadowDy="1.0"  
  100.                     android:shadowRadius="1.0"  
  101.                     android:text="照片"  
  102.                     android:textColor="#ffffffff"  
  103.                     android:textSize="13.0dip" />  
  104.             </LinearLayout>  
  105.   
  106.             <LinearLayout  
  107.                 android:layout_width="wrap_content"  
  108.                 android:layout_height="wrap_content"  
  109.                 android:layout_margin="5dp"  
  110.                 android:layout_weight="1"  
  111.                 android:orientation="vertical" >  
  112.   
  113.                 <ImageView  
  114.                     android:layout_width="wrap_content"  
  115.                     android:layout_height="wrap_content"  
  116.                     android:layout_gravity="center_horizontal"  
  117.                     android:layout_marginTop="1.0dip"  
  118.                     android:src="@drawable/popwindow_picture_btn" />  
  119.   
  120.                 <TextView  
  121.                     android:layout_width="wrap_content"  
  122.                     android:layout_height="wrap_content"  
  123.                     android:layout_gravity="center_horizontal"  
  124.                     android:layout_marginTop="5.0dip"  
  125.                     android:shadowColor="#ff000000"  
  126.                     android:shadowDx="1.0"  
  127.                     android:shadowDy="1.0"  
  128.                     android:shadowRadius="1.0"  
  129.                     android:text="視頻"  
  130.                     android:textColor="#ffffffff"  
  131.                     android:textSize="13.0dip" />  
  132.             </LinearLayout>  
  133.   
  134.             <LinearLayout  
  135.                 android:layout_width="wrap_content"  
  136.                 android:layout_height="wrap_content"  
  137.                 android:layout_margin="5dp"  
  138.                 android:layout_weight="1"  
  139.                 android:orientation="vertical" >  
  140.   
  141.                 <ImageView  
  142.                     android:layout_width="wrap_content"  
  143.                     android:layout_height="wrap_content"  
  144.                     android:layout_gravity="center_horizontal"  
  145.                     android:layout_marginTop="1.0dip"  
  146.                     android:src="@drawable/popwindow_sign_btn" />  
  147.   
  148.                 <TextView  
  149.                     android:layout_width="wrap_content"  
  150.                     android:layout_height="wrap_content"  
  151.                     android:layout_gravity="center_horizontal"  
  152.                     android:layout_marginTop="5.0dip"  
  153.                     android:shadowColor="#ff000000"  
  154.                     android:shadowDx="1.0"  
  155.                     android:shadowDy="1.0"  
  156.                     android:shadowRadius="1.0"  
  157.                     android:text="簽到"  
  158.                     android:textColor="#ffffffff"  
  159.                     android:textSize="13.0dip" />  
  160.             </LinearLayout>  
  161.         </LinearLayout>  
  162.     </LinearLayout>  
  163.   
  164. </RelativeLayout></span>  
3、其中一個Fragment佈局頁面,fragment_1.xml:

[html] view plaincopy
  1. <span style="font-size:12px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent" >  
  5.   
  6.     <ImageView  
  7.         android:id="@+id/imageview"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent"  
  10.         android:scaleType="fitCenter"  
  11.         android:src="@drawable/xianjian01" >  
  12.     </ImageView>  
  13.   
  14. </LinearLayout></span>  
4、自定義按鈕的資源文件,由於比較簡單,就列出其中一個,tab_friendfeed_btn.xml:
[html] view plaincopy
  1. <span style="font-size:12px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.     <item android:drawable="@drawable/toolbar_friendfeed_pressed" android:state_selected="true"/>  
  5.     <item android:drawable="@drawable/toolbar_friendfeed_normal"/>  
  6.   
  7. </selector></span>  
5、主Activity界面,MainActivity.java:
[java] view plaincopy
  1. <span style="font-size:12px;">package com.yangyu.mycustomtab03;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.drawable.BitmapDrawable;  
  5. import android.os.Bundle;  
  6. import android.support.v4.app.FragmentActivity;  
  7. import android.support.v4.app.FragmentTransaction;  
  8. import android.view.Gravity;  
  9. import android.view.LayoutInflater;  
  10. import android.view.MotionEvent;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.view.View.OnTouchListener;  
  14. import android.widget.FrameLayout;  
  15. import android.widget.ImageView;  
  16. import android.widget.LinearLayout;  
  17. import android.widget.PopupWindow;  
  18. import android.widget.PopupWindow.OnDismissListener;  
  19.   
  20. public class MainActivity extends FragmentActivity implements OnClickListener{  
  21.     //定義Fragment頁面  
  22.     private FragmentPage1 fragmentPage1;  
  23.     private FragmentPage2 fragmentPage2;  
  24.     private FragmentPage3 fragmentPage3;  
  25.     private FragmentPage4 fragmentPage4;  
  26.           
  27.     //定義佈局對象  
  28.     private FrameLayout friendfeedFl,myfeedFl,homeFl,moreFl;  
  29.       
  30.     //定義圖片組件對象  
  31.     private ImageView friendfeedIv,myfeedIv,homeIv,moreIv;  
  32.       
  33.     //定義按鈕圖片組件  
  34.     private ImageView toggleImageView,plusImageView;  
  35.       
  36.     //定義PopupWindow  
  37.     private PopupWindow popWindow;  
  38.          
  39.     @Override  
  40.     protected void onCreate(Bundle savedInstanceState) {  
  41.         super.onCreate(savedInstanceState);  
  42.         setContentView(R.layout.activity_main);  
  43.           
  44.         initView();  
  45.           
  46.         initData();  
  47.           
  48.         //初始化默認爲選中點擊了“動態”按鈕  
  49.         clickFriendfeedBtn();  
  50.     }  
  51.   
  52.     /** 
  53.      * 初始化組件 
  54.      */  
  55.     private void initView(){              
  56.         //實例化佈局對象  
  57.         friendfeedFl = (FrameLayout)findViewById(R.id.layout_friendfeed);  
  58.         myfeedFl = (FrameLayout)findViewById(R.id.layout_myfeed);  
  59.         homeFl = (FrameLayout)findViewById(R.id.layout_home);  
  60.         moreFl = (FrameLayout)findViewById(R.id.layout_more);  
  61.           
  62.         //實例化圖片組件對象  
  63.         friendfeedIv = (ImageView)findViewById(R.id.image_friendfeed);  
  64.         myfeedIv = (ImageView)findViewById(R.id.image_myfeed);  
  65.         homeIv = (ImageView)findViewById(R.id.image_home);  
  66.         moreIv = (ImageView)findViewById(R.id.image_more);  
  67.           
  68.         //實例化按鈕圖片組件  
  69.         toggleImageView = (ImageView)findViewById(R.id.toggle_btn);  
  70.         plusImageView = (ImageView)findViewById(R.id.plus_btn);  
  71.     }  
  72.   
  73.     /** 
  74.      * 初始化數據 
  75.      */  
  76.     private void initData(){  
  77.         //給佈局對象設置監聽  
  78.         friendfeedFl.setOnClickListener(this);  
  79.         myfeedFl.setOnClickListener(this);  
  80.         homeFl.setOnClickListener(this);  
  81.         moreFl.setOnClickListener(this);  
  82.   
  83.         //給按鈕圖片設置監聽  
  84.         toggleImageView.setOnClickListener(this);  
  85.     }  
  86.   
  87.     @Override  
  88.     public void onClick(View v) {  
  89.         switch (v.getId()) {  
  90.         //點擊動態按鈕  
  91.         case R.id.layout_friendfeed:  
  92.             clickFriendfeedBtn();  
  93.             break;  
  94.         //點擊與我相關按鈕  
  95.         case R.id.layout_myfeed:  
  96.             clickMyfeedBtn();  
  97.             break;  
  98.         //點擊我的空間按鈕  
  99.         case R.id.layout_home:  
  100.             clickHomeBtn();  
  101.             break;  
  102.         //點擊更多按鈕  
  103.         case R.id.layout_more:  
  104.             clickMoreBtn();  
  105.             break;  
  106.         //點擊中間按鈕  
  107.         case R.id.toggle_btn:  
  108.             clickToggleBtn();  
  109.             break;        
  110.         }  
  111.     }  
  112.       
  113.     /** 
  114.      * 顯示PopupWindow彈出菜單 
  115.      */  
  116.     private void showPopupWindow(View parent){  
  117.         if (popWindow == null) {          
  118.             LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  119.   
  120.             View view = layoutInflater.inflate(R.layout.popwindow_layout,null);  
  121.             // 創建一個PopuWidow對象  
  122.             popWindow = new PopupWindow(view,LinearLayout.LayoutParams.FILL_PARENT, 200);  
  123.         }     
  124.         // 使其聚集 ,要想監聽菜單裏控件的事件就必須要調用此方法  
  125.         popWindow.setFocusable(true);  
  126.         // 設置允許在外點擊消失  
  127.         popWindow.setOutsideTouchable(true);  
  128.         // 設置背景,這個是爲了點擊“返回Back”也能使其消失,並且並不會影響你的背景  
  129.         popWindow.setBackgroundDrawable(new BitmapDrawable());        
  130.         //設置菜單顯示的位置  
  131.         popWindow.showAsDropDown(parent, Gravity.CENTER, 0);  
  132.   
  133.         //監聽菜單的關閉事件  
  134.         popWindow.setOnDismissListener(new OnDismissListener() {              
  135.             @Override  
  136.             public void onDismiss() {  
  137.                 //改變顯示的按鈕圖片爲正常狀態  
  138.                 changeButtonImage();                  
  139.             }  
  140.         });  
  141.           
  142.         //監聽觸屏事件  
  143.         popWindow.setTouchInterceptor(new OnTouchListener() {  
  144.             public boolean onTouch(View view, MotionEvent event) {            
  145.                 //改變顯示的按鈕圖片爲正常狀態  
  146.                 changeButtonImage();  
  147.   
  148.                 return false;                                 
  149.             }  
  150.         });  
  151.     }     
  152.       
  153.     /** 
  154.      * 點擊了“動態”按鈕 
  155.      */  
  156.     private void clickFriendfeedBtn(){  
  157.         //實例化Fragment頁面  
  158.         fragmentPage1 = new FragmentPage1();  
  159.         //得到Fragment事務管理器  
  160.         FragmentTransaction fragmentTransaction = this.getSupportFragmentManager().beginTransaction();  
  161.         //替換當前的頁面  
  162.         fragmentTransaction.replace(R.id.frame_content, fragmentPage1);  
  163.         //事務管理提交  
  164.         fragmentTransaction.commit();  
  165.           
  166.         friendfeedFl.setSelected(true);  
  167.         friendfeedIv.setSelected(true);  
  168.           
  169.         myfeedFl.setSelected(false);  
  170.         myfeedIv.setSelected(false);  
  171.           
  172.         homeFl.setSelected(false);  
  173.         homeIv.setSelected(false);  
  174.           
  175.         moreFl.setSelected(false);  
  176.         moreIv.setSelected(false);  
  177.     }  
  178.       
  179.     /** 
  180.      * 點擊了“與我相關”按鈕 
  181.      */  
  182.     private void clickMyfeedBtn(){  
  183.         // 實例化Fragment頁面  
  184.         fragmentPage2 = new FragmentPage2();  
  185.         // 得到Fragment事務管理器  
  186.         FragmentTransaction fragmentTransaction = this.getSupportFragmentManager().beginTransaction();  
  187.         // 替換當前的頁面  
  188.         fragmentTransaction.replace(R.id.frame_content, fragmentPage2);  
  189.         // 事務管理提交  
  190.         fragmentTransaction.commit();  
  191.           
  192.         friendfeedFl.setSelected(false);  
  193.         friendfeedIv.setSelected(false);  
  194.           
  195.         myfeedFl.setSelected(true);  
  196.         myfeedIv.setSelected(true);  
  197.           
  198.         homeFl.setSelected(false);  
  199.         homeIv.setSelected(false);  
  200.           
  201.         moreFl.setSelected(false);  
  202.         moreIv.setSelected(false);  
  203.     }  
  204.       
  205.     /** 
  206.      * 點擊了“我的空間”按鈕 
  207.      */  
  208.     private void clickHomeBtn(){  
  209.         // 實例化Fragment頁面  
  210.         fragmentPage3 = new FragmentPage3();  
  211.         // 得到Fragment事務管理器  
  212.         FragmentTransaction fragmentTransaction = this.getSupportFragmentManager().beginTransaction();  
  213.         // 替換當前的頁面  
  214.         fragmentTransaction.replace(R.id.frame_content, fragmentPage3);  
  215.         // 事務管理提交  
  216.         fragmentTransaction.commit();  
  217.                   
  218.         friendfeedFl.setSelected(false);  
  219.         friendfeedIv.setSelected(false);  
  220.           
  221.         myfeedFl.setSelected(false);  
  222.         myfeedIv.setSelected(false);  
  223.           
  224.         homeFl.setSelected(true);  
  225.         homeIv.setSelected(true);  
  226.           
  227.         moreFl.setSelected(false);  
  228.         moreIv.setSelected(false);  
  229.     }  
  230.       
  231.     /** 
  232.      * 點擊了“更多”按鈕 
  233.      */  
  234.     private void clickMoreBtn(){  
  235.         // 實例化Fragment頁面  
  236.         fragmentPage4 = new FragmentPage4();  
  237.         // 得到Fragment事務管理器  
  238.         FragmentTransaction fragmentTransaction = this.getSupportFragmentManager().beginTransaction();  
  239.         // 替換當前的頁面  
  240.         fragmentTransaction.replace(R.id.frame_content, fragmentPage4);  
  241.         // 事務管理提交  
  242.         fragmentTransaction.commit();  
  243.           
  244.         friendfeedFl.setSelected(false);  
  245.         friendfeedIv.setSelected(false);  
  246.           
  247.         myfeedFl.setSelected(false);  
  248.         myfeedIv.setSelected(false);  
  249.           
  250.         homeFl.setSelected(false);  
  251.         homeIv.setSelected(false);  
  252.           
  253.         moreFl.setSelected(true);  
  254.         moreIv.setSelected(true);  
  255.     }  
  256.       
  257.     /** 
  258.      * 點擊了中間按鈕 
  259.      */  
  260.     private void clickToggleBtn(){        
  261.         showPopupWindow(toggleImageView);  
  262.         //改變按鈕顯示的圖片爲按下時的狀態  
  263.         plusImageView.setImageResource(R.drawable.toolbar_plusback);  
  264.         toggleImageView.setImageResource(R.drawable.toolbar_btn_pressed);  
  265.     }  
  266.       
  267.     /** 
  268.      * 改變顯示的按鈕圖片爲正常狀態 
  269.      */  
  270.     private void changeButtonImage(){  
  271.         plusImageView.setImageResource(R.drawable.toolbar_plus);  
  272.         toggleImageView.setImageResource(R.drawable.toolbar_btn_normal);  
  273.     }  
  274.           
  275. }</span><span style="font-size:18px;">  
  276. </span>  
6、Fragment其中一個頁面,FragmentPage1.java:

[java] view plaincopy
  1. package com.yangyu.mycustomtab03;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.v4.app.Fragment;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8.   
  9. public class FragmentPage1 extends Fragment{  
  10.   
  11.     @Override  
  12.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  13.             Bundle savedInstanceState) {  
  14.           
  15.         return inflater.inflate(R.layout.fragment_1, null);       
  16.     }     
  17. }  

        到這裏整個項目就基本上講完了,大家還可以爲此項目繼續完善下去,實現點擊菜單選項後實現的效果以及頂部標題欄的實現。對於這個項目,博主還有一個地方不明白,就是針對此項目,在彈出菜單後,有什麼好的簡單一點的方法將背景置爲灰色?我也試了一些方法,但是效果都不是特別的理想,希望讀者有什麼想法可以給博主留言,大家可以互相交流學習一下。


源碼下載地址




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