1. 程式人生 > >Android底部tab與標題欄相結合

Android底部tab與標題欄相結合

Android底部tab切換介面的實現比較簡單,可以利用TabHost直接實現,實現方式網上資源很多。那麼除了用特定的元件來實現tab外能不能自己寫程式碼實現呢。答案是肯定的。還有一個很常用的問題,就是不同的tab介面能否實現不同的標題欄?這個需求在專案中經常碰到,本文將講敘Android底部tab切換介面的實現以及它與標題欄的結合。實現效果圖如下:


上圖就是我們要實現的效果圖,切換到不同的介面有不同的顯示標題。不多說直接上程式碼。

首先當然是佈局檔案,先實現標題欄佈局。新建title.xml檔案

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="#111111" >


    <TextView
        android:id="@+id/title_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:textColor="#ffffff"
        android:textSize="18dp"
        android:visibility="gone" />


    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:layout_centerInParent="true"
        android:textColor="#ffffff"
        android:textSize="22dp"/>


    <TextView
        android:id="@+id/title_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:textColor="#ffffff"
        android:textSize="18dp"
        android:visibility="gone" />


</RelativeLayout>

以上實現了標題欄的佈局,title_left和title_right分別是標題欄的左、右按鈕,預設情況下將其設定為不可見。

接下來就是tab欄佈局檔案的實現,新建一個tab.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_bottom"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="#efefef"
    android:orientation="horizontal" >


    <LinearLayout
        android:id="@+id/nav_search"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="vertical" >


        <View
            android:id="@+id/nav_search_color"
            android:layout_width="fill_parent"
            android:layout_height="3dp"
            android:background="@color/nav_pressed"
            android:duplicateParentState="true" />


        <ImageView
            android:id="@+id/nav_search_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="3dp"
            android:scaleType="center"
            android:src="@drawable/icon_square_nor1" />


        <TextView
            android:id="@+id/nav_search_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="搜  索"
            android:textColor="@color/nav_pressed"
            android:textSize="10sp" />
    </LinearLayout>


    <LinearLayout
        android:id="@+id/nav_home_layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="vertical" >


        <View
            android:id="@+id/nav_home_color"
            android:layout_width="fill_parent"
            android:layout_height="3dp"
            android:background="@color/nav_normal" />


        <ImageView
            android:id="@+id/nav_home_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="3dp"
            android:scaleType="center"
            android:src="@drawable/icon_home_nor" />


        <TextView
            android:id="@+id/nav_home_text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="主  頁"
            android:textColor="@color/nav_text_normal"
            android:textSize="10sp" />
    </LinearLayout>


    <LinearLayout
        android:id="@+id/nav_selfinfo_layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="vertical" >


        <View
            android:id="@+id/nav_selfinfo_color"
            android:layout_width="fill_parent"
            android:layout_height="3dp"
            android:background="@color/nav_normal" />


        <ImageView
            android:id="@+id/nav_selfinfo_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="3dp"
            android:scaleType="center"
            android:src="@drawable/icon_selfinfo_nor" />


        <TextView
            android:id="@+id/nav_selfinfo_text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="個人資訊"
            android:textColor="@color/nav_text_normal"
            android:textSize="10sp" />
    </LinearLayout>


    <LinearLayout
        android:id="@+id/nav_mess_layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="vertical" >


        <View
            android:id="@+id/nav_mess_color"
            android:layout_width="fill_parent"
            android:layout_height="3dp"
            android:background="@color/nav_normal" />


        <ImageView
            android:id="@+id/nav_mess_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="3dp"
            android:scaleType="center"
            android:src="@drawable/icon_meassage_nor" />


        <TextView
            android:id="@+id/nav_mess_text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="3dp"
            android:gravity="center_horizontal"
            android:text="信   息"
            android:textColor="@color/nav_text_normal"
            android:textSize="10sp" />
    </LinearLayout>


</LinearLayout>

只需要看一部分就可以了,一個LinearLayout包括著一個View,ImageView和TextView,其中View是底部欄上方的那條橫線。ImagView與TextView不用說,就是底部欄的頭像和頭像下方的文字。

主佈局檔案就是利用include把標題欄與底部欄融合到一起,並用ViewPage來顯示不同頁面。佈局如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >


    <include
        android:id="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        layout="@layout/title" />
         
    <include
        android:id="@+id/tab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        layout="@layout/tab" />
     <LinearLayout
        android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:layout_below="@id/top"    
    android:layout_above="@id/tab"       
        android:orientation="vertical" >     
        <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" > 
        </android.support.v4.view.ViewPager>  
    </LinearLayout>  


</RelativeLayout>


接下來的實現我們用到了Fragment,四個頁面分別載入了四個Fragment。所以要寫四個不同的fragment.xml,這個根據不同需求進行編寫。那重頭戲來了,下面貼出主程式。程式碼有點長,但不用擔心,其實現原理很簡單。

public class MainActivity extends FragmentActivity {
private ViewPager m_vp; 
private int navIndex = 0; 
private int currIndex = 0;
private Fragment mfragment1;
private Fragment mfragment2;
private Fragment mfragment3;
private Fragment mfragment4;
private List<Fragment> fragmentList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initTitle();
getView();
initFragments();
}
/**
* 初始化標題,並載入四個Fragment
*/
public void initTitle(){
((TextView) findViewById(R.id.title)).setText("搜尋");
((TextView)findViewById(R.id.title_left)).setVisibility(View.INVISIBLE);
((TextView)findViewById(R.id.title_right)).setVisibility(View.INVISIBLE);
}
private void getView(){
m_vp = (ViewPager)findViewById(R.id.viewpager);
mfragment1 = new Fragment1();
mfragment2 = new Fragment2();
mfragment3 = new Fragment3();
mfragment4 = new Fragment4();
}
/**
* 初始化頁面
*/
public void initFragments()
{
fragmentList = new ArrayList<Fragment>();
fragmentList.add(mfragment1);
fragmentList.add(mfragment2);
fragmentList.add(mfragment3);
fragmentList.add(mfragment4);
findViewById(R.id.nav_search).setOnClickListener(new MyOnClickListener(0));
findViewById(R.id.nav_home_layout).setOnClickListener(new MyOnClickListener(1));
findViewById(R.id.nav_selfinfo_layout).setOnClickListener(new MyOnClickListener(2));
findViewById(R.id.nav_mess_layout).setOnClickListener(new MyOnClickListener(3));
setNavState(navIndex, 0);    
m_vp.setAdapter(new MyViewPagerAdapter(getSupportFragmentManager()));
m_vp.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
Log.i("zhi", arg0+"");
switch (arg0) {
case 0:
if (navIndex != 0){
// 點選之前如果就是這個控制元件,不變化
setNavState(navIndex, 0);
navIndex = 0;
}
break;
case 1:
if (navIndex != 1){
// 點選之前如果就是這個控制元件,不變化
setNavState(navIndex, 1);
navIndex = 1; 
}
break;
case 2:
if (navIndex != 2){
// 點選之前如果就是這個控制元件,不變化
setNavState(navIndex, 2);
navIndex = 2;
}
break;
case 3:
if (navIndex != 3){
// 點選之前如果就是這個控制元件,不變化
setNavState(navIndex, 3);
navIndex = 3; 
}
break;
default:
break;
} 
}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}

@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
private void setNavState(int before, int after) {
// 點選之前的那個按鈕恢復正常
switch (before) {

case 0:
findViewById(R.id.nav_search_color).setBackgroundResource(R.color.nav_normal);
((ImageView )findViewById(R.id.nav_search_img)).setImageResource(R.drawable.icon_square_nor);
((TextView) findViewById(R.id.nav_search_text)).setTextColor(getResources().getColor(R.color.nav_text_normal));
break;
case 1:
findViewById(R.id.nav_home_color).setBackgroundResource(R.color.nav_normal);
((ImageView )findViewById(R.id.nav_home_img)).setImageResource(R.drawable.icon_home_nor);
((TextView) findViewById(R.id.nav_home_text)).setTextColor(getResources().getColor(R.color.nav_text_normal));
((TextView)findViewById(R.id.title_right)).setVisibility(View.INVISIBLE);
break;
case 2:
findViewById(R.id.nav_selfinfo_color).setBackgroundResource(R.color.nav_normal);
((ImageView )findViewById(R.id.nav_selfinfo_img)).setImageResource(R.drawable.icon_selfinfo_nor);
((TextView) findViewById(R.id.nav_selfinfo_text)).setTextColor(getResources().getColor(R.color.nav_text_normal));
((TextView)findViewById(R.id.title_left)).setVisibility(View.INVISIBLE);
break;
case 3:
findViewById(R.id.nav_mess_color).setBackgroundResource(R.color.nav_normal);
((ImageView )findViewById(R.id.nav_mess_img)).setImageResource(R.drawable.icon_meassage_nor);
((TextView) findViewById(R.id.nav_mess_text)).setTextColor(getResources().getColor(R.color.nav_text_normal));
break;
default:
break;
}
// 點選到的那個顯示狀態
switch (after) {

case 0:
findViewById(R.id.nav_search_color).setBackgroundResource(R.color.nav_pressed);
((ImageView )findViewById(R.id.nav_search_img)).setImageResource(R.drawable.icon_square_nor1);
((TextView) findViewById(R.id.nav_search_text)).setTextColor(getResources().getColor(R.color.nav_text_pressed));
((TextView) findViewById(R.id.title)).setText("搜尋");
break;
case 1:
findViewById(R.id.nav_home_color).setBackgroundResource(R.color.nav_pressed);
((ImageView )findViewById(R.id.nav_home_img)).setImageResource(R.drawable.icon_home_nor1);
((TextView) findViewById(R.id.nav_home_text)).setTextColor(getResources().getColor(R.color.nav_text_pressed));
((TextView)findViewById(R.id.title_right)).setVisibility(View.VISIBLE);
((TextView)findViewById(R.id.title_right)).setText("設定");
((TextView) findViewById(R.id.title)).setText("主頁");
break;
case 2:
findViewById(R.id.nav_selfinfo_color).setBackgroundResource(R.color.nav_pressed);
((ImageView )findViewById(R.id.nav_selfinfo_img)).setImageResource(R.drawable.icon_selfinfo_sel1);
((TextView) findViewById(R.id.nav_selfinfo_text)).setTextColor(getResources().getColor(R.color.nav_text_pressed));
((TextView)findViewById(R.id.title_left)).setVisibility(View.VISIBLE);
   ((TextView)findViewById(R.id.title_left)).setText("返回");
((TextView) findViewById(R.id.title)).setText("個人資訊");
break;
case 3:
findViewById(R.id.nav_mess_color).setBackgroundResource(R.color.nav_pressed);
((ImageView )findViewById(R.id.nav_mess_img)).setImageResource(R.drawable.icon_meassage_nor1);
((TextView) findViewById(R.id.nav_mess_text)).setTextColor(getResources().getColor(R.color.nav_text_pressed));
((TextView) findViewById(R.id.title)).setText("資訊");
break;
default:
break;
}
}

public class MyViewPagerAdapter extends FragmentPagerAdapter{
public MyViewPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}

@Override
public Fragment getItem(int arg0) {
return fragmentList.get(arg0);
}






@Override
public int getCount() {
return fragmentList.size();
}

}

public class MyOnClickListener implements View.OnClickListener {
private int index = 0;


public MyOnClickListener(int i) {
index = i;
currIndex=i;

}
@Override
public void onClick(View v) {
m_vp.setCurrentItem(index);


}
}; 

}


相信大家都能看懂,這裡簡單講解一下。程式首先執行initTitle對標題欄進行初始化,在getView中定義一個list裝上四個Fragment,方便之後的呼叫。initFragments()方法主要是完成對介面的載入和監聽不到的觸發事件。這裡用到了Viewpage進行頁面的切換。首先是監聽底部欄按鈕的的點選事件,點選的按鈕觸發ViewPage的setCurrentItem方法載入不同的ViewPage頁。ViewPage對頁面的變化進行監聽,不同頁面呼叫不同的setNavState()方法。最後就只剩下瞭解setNavState的作用了。setNavState方法用於對底部按鈕的圖示、字型狀態還有標題欄的顯示狀態進行動態變化,switch(before)是操作之前點選的那個按鈕的狀態,switch(after)是操作之後點選的按鈕的狀態。

到這裡主函式就講完了。接下來要做是的就定義自己的Fragement,讓它們去實現我們想要的介面。至此我們一個自定義的底tab介面就完成了。是不是很簡單。以下我貼出原始碼,下載不用積分,只希望對大家有點幫助。

程式碼下載

相關推薦

Android底部tab標題結合

Android底部tab切換介面的實現比較簡單,可以利用TabHost直接實現,實現方式網上資源很多。那麼除了用特定的元件來實現tab外能不能自己寫程式碼實現呢。答案是肯定的。還有一個很常用的問題,就是不同的tab介面能否實現不同的標題欄?這個需求在專案中經常碰到,本文將講

Android狀態列標題風格一致

首先只有Android4.4也就是api19及以上才可以使用沉浸式狀態列。 第一步:在res目錄下新建一個values-v19資料夾 新建一個style.xml,新增程式碼 <resources  <style name="AppBaseTheme"

Android】自定義標題底部

為了簡化起見,只寫關鍵屬性,具體需要可以自己慢慢調 頂部標題title_layout.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:backg

Android ViewPager+HorizontalScrollView實現標題滑動(騰訊新聞)

flat off net 單位 上一個 undle scrollto 一起 ava 1) ViewPager提供了左右滑動切換頁面的方法,但是它所提供的標題只是無語,估計沒有真正的項目會照搬拿過來;並且它只能一頁一頁滑,我想直接查看最後一頁要滑半天; 2) 看了騰訊新聞客

android 隱藏默認標題兩種方式

feature set body eat androi get featrue blog title 在setconnetView()之前加入 requestWindowFeature(Window.FEATURE_NO_TITLE);//第一種 getWindow().s

Android開發如何去除標題title

  去除標題欄title其實非常簡單,他有兩種方法,一種是在程式碼中新增,另一種是在AndroidManifest.xml中新增: 1、在程式碼中實現: 在此方法setContentView(R.layout.main)之前加

子類的構造方法(super關鍵字結合

1、 隱式呼叫:子類構造方法沒有顯示呼叫父類構造方法(子類中沒有super()),那麼父類必須顯示提供無參構造方法。 這個時候,系統必須在父類中顯示呼叫無參建構函式。 顯示呼叫: 2、例項1: 在calcPerimeter方法中使用

左邊側滑標題圖示互動

Xml: <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout android:id="@+id/drawer" xmlns:android=“http:/

二十分鐘教你如何將區塊鏈應用函式計算結合

前言 本篇文章適合對區塊鏈應用感興趣或是想要通過函式計算服務進一步開發區塊鏈應用的新人。本文將結合阿里雲區塊鏈服務、阿里雲函式計算服務、阿里雲日誌服務 以及社群應用 Marbles,手把手教大家如何將阿里雲區塊鏈服務與阿里雲函式計算服務相結合,並進一步提供業務上的結合場景,供大家開拓思路。 本文分為以下幾

Android關於滾動View標題漸變的解決方案

這個工具類是在做某電商專案的時候通宵搞出來的,所以分享給各位開發者,避免無效的加班 /** * @文件說明: 處理滾動view的漸變效果 * @開發者: 江榮濤 * @建立時間: 2018/6/13 0:34 **/ public class Scroll

Android 頂部灰條標題不顯示的方法

Android應用開發介面頂部上有灰條,用來顯示專案名稱。這個應用名稱是可以更改的,在這個字串值裡就可以設定 <string name="app_name">應用名稱</string>。那麼如何能做到不顯示呢?加上一個語句就行了:requestWi

[Android] 自定義頂部標題

思路及實現步驟 1.定義標題欄佈局 2.自定義TitleActivity控制標題欄按鈕監聽 3.在TitleActivity中實現標題欄以下內容切換 效果如下: 首先定義標題欄 layout_title.xml <?xml v

Android開發中去掉標題

方法一: requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); 注意:1、在這個方法中requestWindowFeature必須在setContentVi

Android Studio App隱藏標題

IDE是Android Studio。 1.把MainActivity.java檔案裡的 public class MainActivity extends ActionBarActivity 改為

MFC隱藏和顯示工作列標題

一、對工作列的操作 void CVideoDlg::HideTaskBar(BOOL bHide) { //int nCmdShow; LPARAM lParam;if(bHide == TRUE) //為TRUE時,自動隱藏{nCmdShow = SW_HIDE;//lP

Android webview隱藏頂部標題和頂部空白margin

h5的頂部是這樣的 當我用webview載入的時候需要將更多這一片區域隱藏,同事更多下面這一塊距離頂部的margin也要隱藏(因為我將頂部隱藏掉後出現了大塊空白),已知h5頁面的程式碼是這樣:(谷歌瀏覽器按F12) 我們需要做的就是隱藏著兩個部分: 在webclient進行處理:

RSADiffie-Hellman結合的保密通訊

最近由於工作需要在思考身份驗證和保密通訊 ,把它記錄下來.Deffie-Hellman 金鑰交換演算法最大的缺點的存在中間人攻擊的弱點,Deffie-Hellman簡述如下: 前提:約定一個大素數p,一個小於p的大整數g.A: X = gx mod p; x是一隨機整數.B:

android UI 的去標題和去狀態列(全屏)

Android 設定隱藏標題欄和狀態列有兩種方法: 第一:在程式碼中實現 PS:設定隱藏標題欄和狀態列的程式碼一定要寫在 setContentView(R.layout.activity_main)前面,否則會報錯。 protected void onCreate(Bund

Android設定activity無標題

一、通過Java程式碼 在setContentView之前執行: requestWindowFeature(Window.FEATURE_NO_TITLE);//隱藏標題欄 getWindow().setFlags(WindowManager.LayoutParams.FL

Android應用中去掉標題方法總結(Eclipse+Android Studio)

Eclipse 1.在程式程式碼中實現 需要注意的是: this.requestWindowFeature(Window.FEATURE_NO_TITLE);