1. 程式人生 > >Android裡的底部頁面的切換的實現的三種方式

Android裡的底部頁面的切換的實現的三種方式

底部頁面的切換

實現方式有三種:

1. tabhost+Activity的方式:

1.將MainActivity繼承TabActivity

2.在setContentView();(不一定緊跟隨)之後獲取TabHost物件<只有繼承了TabActivity才能獲取得到TabHost物件> getTabHost()

3.建立Intent物件用來顯示每個Tab頁面.

4.建立tabSpec物件用來新增進tabHost裡的:

    TabSpec tabSpec1 = tabHost.newTabSpec("two").setIndicator("",
                            getResources().getDrawable(R.drawable.home_press2))
                        .setContent(intent1);

其中:設定標籤和圖示(setIndicator),設定內容(setContent)

5.新增進tabHost裡

tabHost.add(tabSpec1);

6.注意的是對main.xml檔案的編寫:tabHost使用了特殊的標籤id定義,在main.xml檔案一定是tabHost標籤包裹起來的,注意id:

 <TabHost 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" 
          android:id="@android:id/tabhost">
<!--注意這的id:tabhost,tabs,tabcontent都是特有的一定要有,否則回執行異常-->
<LinearLayout android:orientation="vertical"
              android:layout_width="fill_parent" android:layout_height="fill_parent">

<TabWidget android:id="@android:id/tabs"
           android:layout_alignParentBottom="true"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"/>

<FrameLayout
          android:id="@android:id/tabcontent"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent" />

</LinearLayout>
</TabHost>

7.關於tabhost+Activity的方式:這種方式以及廢棄掉了,因為實現它需要一個介面一個Activity,在Android中Activity屬於重量級空間,最好不輕易使用,所以拋棄掉了.

2.fragment巢狀fragment:

  1. 在早期開發過程中,fragment是不能巢狀fragment的。
  2. 是在4.2以後才有的,然後在v13包裡面才有這個方法。
  3. 如何使用fragment巢狀fragment的開發方式的話,佈局層次會巢狀太深,導致程式崩掉

3.ViewPage:

1.使用Frgment和Viewpage實現標籤佈局(沒有用框架的)

1.因為需要用到Frgment所以MainActivity繼承下FrgmentActivity

2.因為要需要在操作欄中新增標籤,必須要實現ActionBar.TabListener這個類--->可能會不能自動導包

import android.app.ActionBar;
public class MainActivity extends FragmentActivity implements ActionBar.TabListener{
    //一下是需實現ActionBar.TabListener介面的方法
    public void onTabSelected(Tab tab, FragmentTransaction ft) {}
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {}
    public void onTabReselected(Tab tab, FragmentTransaction ft) {}
}

3.MainActivity佈局檔案的編寫(注意,這個檔案除了viewpage其他元素都不寫):

<android.support.v4.view.ViewPager 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"
android:id="@+id/main_vp"
tools:context=".MainActivity" >
</android.support.v4.view.ViewPager>

4.建立便籤碎片(tab fragment)提供檢視的FragmentPagerAdapter類(介面卡)

class TabsPagerAdapter extends FragmentPagerAdapter{

        public TabsPagerAdapter(FragmentManager fm) {
            super(fm);
            // TODO Auto-generated constructor stub
        }

        @Override
        public Fragment getItem(int arg0) {
            switch(arg0){
            case 0:
                return new TopRatedFragment();
            case 1:
                return new GameFragment();
            case 2:
                return new MovesFragment();
            }
            return null;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return 3;
        }

}

5.為ActionBar新增Tab在不使用其他UI元素,譬如TabHost的前提下顯示標籤,其實在操作欄中有一個內建的功能,那就是新增標籤。很簡單的,我們需要做的就是允許操作欄使用setNavigationMode(ActionBar.NAVIGATIONMODETABS)方法。

private String[] tabs = {"Top Rated", "Games", "Movies"};
private ActionBar actionBar;
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab()
        .setText(tab_name)
        .setTabListener(this));//先前已經實現了該監聽介面
} 

6.為標籤新增檢視

//寫個基本的Fragment的類,為TopRatedFragment(),GameFragment(),MovesFragment()提供方便,它們就可以直接實現initData(),initView()方法就得了.
public abstract class BaseFragment extends Fragment {

private String TAG = "BaseFragment";

public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    Log.i(TAG, this.getClass().getSimpleName()+"====onCreate");
    super.onCreate(savedInstanceState);
}
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
    Log.i(TAG, this.getClass().getSimpleName()+"====onCreateView");
        View view = initView();

        return view;
    }

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
    Log.i(TAG, this.getClass().getSimpleName()+"====onActivityCreated");
    initData();
        super.onActivityCreated(savedInstanceState);
    }
public abstract void initData();
public abstract View initView();
}
2.用框架SlidingMenu實現

1.SlidingMenu屬性的一些介紹:

menu.setMode(SlidingMenu.LEFT);//設定左滑選單
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);//設定滑動的螢幕範圍,該設定為全屏區域都可以滑動
menu.setShadowDrawable(R.drawable.shadow);//設定陰影圖片
menu.setShadowWidthRes(R.dimen.shadow_width);//設定陰影圖片的寬度
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);//SlidingMenu劃出時主頁面顯示的剩餘寬度
menu.setBehindWidth(400);//設定SlidingMenu選單的寬度
menu.setFadeDegree(0.35f);//SlidingMenu滑動時的漸變程度
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);//使SlidingMenu附加在Activity上
menu.setMenu(R.layout.menu_layout);//設定menu的佈局檔案
menu.toggle();//動態判斷自動關閉或開啟SlidingMenu
menu.showMenu();//顯示SlidingMenu
menu.showContent();//顯示內容
menu.setOnOpenListener(onOpenListener);//監聽slidingmenu開啟
關於關閉menu有兩個監聽,簡單的來說,對於menu close事件,一個是when,一個是after 
menu.OnClosedListener(OnClosedListener);//監聽slidingmenu關閉時事件
menu.OnClosedListener(OnClosedListener);//監聽slidingmenu關閉後事件
左右都可以劃出SlidingMenu菜單隻需要設定
menu.setMode(SlidingMenu.LEFT_RIGHT);屬性,然後設定右側選單的佈局檔案
menu.setSecondaryShadowDrawable(R.drawable.shadowright);//右側選單的陰影圖片
設定SlidingMenu屬性
sm = getSlidingMenu();
//如果只顯示左側選單就是用LEFT,右側就RIGHT,左右都支援就LEFT_RIGHT
sm.setMode(SlidingMenu.LEFT_RIGHT);//設定選單滑動模式,選單是出現在左側還是右側,還是左右兩側都有
sm.setShadowDrawable(R.drawable.shadow);//設定陰影的圖片資源
sm.setShadowWidthRes(R.dimen.shadow_width);//設定陰影圖片的寬度
//sm.setBehindWidth(200);//設定選單的寬
sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);//SlidingMenu劃出時主頁面顯示的剩餘寬度
sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);//設定滑動的區域
支援右側劃出選單:
//SlidingMenu可以同時支援劃出左右兩側的選單,互不衝突,而且動畫優美,體驗良好。
sm.setSecondaryMenu(R.layout.menu_frame2);//設定右側選單
sm.setSecondaryShadowDrawable(R.drawable.shadowright);//設定右側選單陰影的圖片資源
//右側SlidingMenu的Fragment
getSupportFragmentManager().beginTransaction().replace(R.id.menu_frame2, new SampleListFragment()).commit();
slidingMenu = getSlidingMenu();
//設定是左滑還是右滑,還是左右都可以滑
slidingMenu.setMode(SlidingMenu.LEFT_RIGHT);
//設定陰影寬度
slidingMenu.setShadowWidth(getWindowManager().getDefaultDisplay().getWidth() / 40);
//設定左選單陰影圖片
slidingMenu.setShadowDrawable(R.drawable.shadow);
//設定右選單陰影圖片
slidingMenu.setSecondaryShadowDrawable(R.drawable.right_shadow);
//設定選單佔螢幕的比例
slidingMenu.setBehindOffset(getWindowManager().getDefaultDisplay().getWidth() / 5);
//設定滑動時選單的是否淡入淡出
slidingMenu.setFadeEnabled(true);
//設定淡入淡出的比例
slidingMenu.setFadeDegree(0.4f);
//設定滑動時拖拽效果
slidingMenu.setBehindScrollScale(0);
//設定要使選單滑動,觸碰螢幕的範圍
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

2.應用: 1.將Activity頁繼承SlidingFragmentActivity 2.設定邊側拉出選單佈局和主頁面內容佈局 3.寫個dimens.xml檔案,用來配置SlidingMenu的一些配置.

<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="slidingmenu_offset">180dp</dimen>
<dimen name="list_padding">10dp</dimen>
<dimen name="shadow_width">5dp</dimen>
<dimen name="bottom_tab_padding_drawable">2.0dip</dimen>
<dimen name="bottom_tab_padding_up">2.0dip</dimen>
<dimen name="bottom_tab_font_size">14.0sp</dimen>

</resources>

4.寫個shadow.xml檔案用來配置陰影的一些資訊

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:endColor="#5A000000"
android:centerColor="#2D000000"
android:startColor="#00000000" />
</shape>

5.初始化(配置)下 ---獲取側邊欄物件 ---設定成左側...

    slidingMenu = getSlidingMenu();
    slidingMenu.setMode(SlidingMenu.LEFT);
    slidingMenu.setShadowWidthRes(R.dimen.shadow_width);//陰影寬度
    slidingMenu.setShadowDrawable(R.drawable.shadow);//陰影Drawable
    slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);//拉開後離邊框距離
    slidingMenu.setFadeDegree(0.35f); //顏色漸變比例
    //slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN); //拉動事件區域  --全屏
    slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); //拉動事件區域  --全屏

相關推薦

搭建FTP服務,實現方式的訪問:1.匿名訪問;2,本地用戶訪問;3虛擬用戶訪問。

行程 工作環境 能夠 ftp用戶 a10 linu 進入 編輯 binary FTP服務(File Transfer Protocol,文件傳輸協議)是典型的C/S結構 的應用層協議,需要由服務端軟件,客戶端軟件兩部分共同實 現文件

android studio 新增控制元件的方式

寫在這裡以作筆記。 第一種:佈局檔案中新增 佈局檔案中: <android.support.percent.PercentFrameLayout android:layout_width="match_parent" android:layout_hei

android傳送郵件mail的幾方式

在android裡進行郵件客戶端開發可以有兩種方式: 在郵件客戶端的設計中,可以採用兩種方法。 l 一種是呼叫android系統自帶的郵件服務 優點:這種方法比較簡單易用 缺點:傳送郵件的賬號必須是gmail賬號 l 令一種方法是採用javamail功能包

Android保持螢幕常亮的方式

方式一: 1.新增許可權 <uses-permission android:name="android.permission.WAKE_LOCK"/> 2.獲取PowerManager物件 3.通過PoverManager物件獲取螢幕鎖WakeL

微信小程式頁面跳轉方式

為了不讓使用者在使用小程式時造成困擾,微信小程式規定頁面路徑只能是五層,請儘量避免多層級的互動方式。 頁面跳轉的話就涉及到了多個頁面層級 第一種:wx.navigateTo(OBJECT) 保留當前頁面,跳轉到應用內的某個頁面,使用wx.navigateB

android】繪製圓環的方式

在android中要繪製圓環,暫時知道有三種方式。分別是: 一、設定畫筆的style為stoke,繪製圓 這裡是先繪製內圓,然後繪製圓環(圓環的寬度就是paint設定的paint.setStrokeWidth的寬度),最後繪製外圓。 請看核心原始碼: [ja

android 自定義控制元件的方式

如果說要按型別來劃分的話,自定義View的實現方式大概可以分為三種,自繪控制元件、組合控制元件、以及繼承控制元件。那麼下面我們就來依次學習一下,每種方式分別是如何自定義View的。 一、自繪控制元件 PS: 這篇《自定義View系列》部落格,博主

React ES6元件繫結this的方式

React可以使用React.createClass、ES6 classes、純函式3種方式構建元件。使用React.createClass會自動繫結每個方法的this到當前元件,但使用ES6 classes或純函式時,就要靠手動繫結this了。 bind

Android中解析json資料的方式

Json簡介 JAVAScript Object Notation是一種輕量級的資料交換格式 具有良好的可讀和便於快速編寫的特性。 業內主流技術為其提供了完整的解決方案(有點類似於正則表示式 ,獲得了

Android使用者登入資料儲存的方式

登入的頁面: 佈局程式碼: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.co

Android底部頁面切換實現方式

底部頁面的切換 實現方式有三種: 1. tabhost+Activity的方式: 1.將MainActivity繼承TabActivity 2.在setContentView();(不一定緊跟隨)之後獲取TabHost物件<只有繼承了TabActivity才能獲取得

方式實現Android頁面底部導航欄

我們在Android手機上使用新浪微博和QQ等一些軟體時,經常會遇到類似下面這種頁面底部導航欄的控制元件,使用這種導航欄可以在手機螢幕的一頁中顯示儘可能多的內容,如下圖所示: 下面我將實現這種導航欄的三種方法總結如下: 一、使用TabHost實現(TabHost在新版的A

方式實現自定義圓形頁面載入中效果的進度條,包含一個好看的Android UI

效果圖如下:下載地址 樣式一、通過動畫實現定義res/drawable/loading.xml如下: <?xml version="1.0" encoding="UTF-8"?> ​ <animation-list android:oneshot=

Android 方式實現自定義圓形頁面載入中效果的進度條

一、通過動畫實現定義res/anim/loading.xml如下:<?xml version="1.0" encoding="UTF-8"?>  <animation-list android:oneshot="false"xmlns:android="ht

Android】一、Progress進度條實現方式:主執行緒實現,Service載入,動態建立

前言 更新版本,上傳資料到服務端,都是需要進度顯示的,Android進度顯示兩種方式 ProgressDialog 和 ProgressBar 新版本中ProgressDialog不被推薦使用,所以專案採用ProgressBar 分為三種實現方式: 1、MainAct

Android-響應按鈕的點選實現方法

一、使用反射方式來實現按鈕的點選 新增一個onButtonClicked方法 public void onButtonClicked(View view) { TextView tex

[Android]應用語言切換方法

    Android對國際化與多語言切換已經做得不錯了,一個應用只要命名相應語系的values-[language]資料夾,通過“設定”→“語言&鍵盤”→“選擇語言”即可實現應用多種語言的切換。        但如何在應用裡自己實現?搜尋過發現網上有如下的做法:

Android 方式實現圓形ImageView

所有方式均繼承了ImageView 圓形圖片實現一:BitmapShader package com.open.widget; import android.content.Context; import android.graphics.Bitmap; impor

Android進度條】方式實現自定義圓形進度條ProgressBar

總結了3種方法: 1.多張圖片切換 2.自定義顏色 3.旋轉自定義圖片 其它: Android自定義控制元件NumberCircleProgressBar(圓形進度條)的實現:點選開啟連結 橫線帶數字進度條:點選開啟連結

android方式實現自由移動的view

public class MoveView extends ImageView { private int x, y; private int r, l, t, b; public MoveView(Context context) { this(context, n