Android巧用ActionBar實(shí)現(xiàn)tab導(dǎo)航效果
本文實(shí)例為大家分享了ActionBar實(shí)現(xiàn)tab導(dǎo)航效果的具體代碼,供大家參考,具體內(nèi)容如下
先來說一說基礎(chǔ)知識:
一、基本使用方法
1.獲取ActionBar
獲取actionbar很簡單,在activity中,ationbar=this.getActionBar();
2.顯示/隱藏
設(shè)置actionBar的顯示/隱藏,可以使用show()和hide()方法。
3.設(shè)置MenuItem
通過設(shè)置menuItem,可以使menuItem成為actionbar上的item。
setShowAsAction(int actionenum),這個actionenum支持的參數(shù)如下:
SHOW_AS_ACTION_ALWAYS: 總是將該menuitem顯示在actionbar上
SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW:將該ActionView折疊成普通菜單項(xiàng)
SHOW_AS_ACTION_IF_ROOM:當(dāng)actionbar位置足夠時才顯示在actionbar上。
SHOW_AS_ACTION_NEVER:不將該menuitem顯示在actionbar上。
SHOW_AS_ACTION_WITH_TEXT:把menuItem顯示在actionbar上,并且顯示該菜單項(xiàng)的文本。
也可以在xml屬性,定義item的屬性 android:showAsAction來設(shè)置。
4.啟用程序圖標(biāo)導(dǎo)航
setDisplayHomeAsUpEnabled(boolean showHomeAsUp):設(shè)置是否應(yīng)該將應(yīng)用程序圖標(biāo)轉(zhuǎn)變成可點(diǎn)擊的圖標(biāo),并且在圖標(biāo)上添加一個向左的箭頭。
setDisplayOptions(int options):控制actionbar的顯示選項(xiàng)。opitions選項(xiàng)為:
DISPLAY_HOME_AS_UP
DISPLAY_SHOW_CUSTOM
DISPLAY_SHOW_HOME
DISPLAY_SHOW_TITLE
DISPLAY_USE_LOGO
NAVIGATION_MODE_LIST
NAVIGATION_MODE_STANDARD
NAVIGATION_MODE_TABS
setDislayShowHomeEnabled(boolean showHome):設(shè)置是否顯示應(yīng)用程序的圖標(biāo)。
setHomeButtonEnabled(boolean eabled): 設(shè)置是否將應(yīng)用程序圖標(biāo)轉(zhuǎn)變成可點(diǎn)擊的按鈕。
5.actionbar中添加view
定義Action Item時,使用android:actionViewClass屬性指定Action View的實(shí)現(xiàn)類。
定義Action Item時,使用android:actionLayout屬性指定Action View對應(yīng)的視圖資源。
二、Android巧用ActionBar實(shí)現(xiàn)tab導(dǎo)航效果
利用actionbar同樣也可以輕松的實(shí)現(xiàn)tab導(dǎo)航的效果,配合使用fragment實(shí)現(xiàn)切換不同view的功能。
若想使用這個功能,需要
1)設(shè)置actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS),使actionbar使用tab導(dǎo)航功能。
2)調(diào)用actionbar的addTab()方法,添加多個tab標(biāo)簽,并為每個tab標(biāo)簽添加時間監(jiān)聽器。
MyFragment.java
package com.app.main;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;
@SuppressLint("NewApi")
public class MyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Context context = this.getActivity();
TextView tv = new TextView(context);
Bundle arc = this.getArguments();
int tabs=arc.getInt("key");
tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tv.setText("hello actionbar "+tabs);
return tv;
}
}
main.xml
<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=".Main" > <LinearLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" /> </RelativeLayout>
Main.java
package com.app.main;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
public class Main extends Activity implements ActionBar.TabListener {
ActionBar actionBar = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
actionBar = this.getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab().setText("tab1")
.setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("tab2")
.setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("tab3")
.setTabListener(this));
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
MyFragment frag = new MyFragment();
int index = tab.getPosition() + 1;
Bundle bundle = new Bundle();
bundle.putInt("key", index);
frag.setArguments(bundle);
FragmentTransaction action = Main.this.getFragmentManager()
.beginTransaction();
action.replace(R.id.container, frag);
action.commit();
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
實(shí)現(xiàn)效果:


以上就是本文的全部內(nèi)容,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Android自定義ActionBar實(shí)例
- Android中ActionBar以及menu的代碼設(shè)置樣式
- android中開啟actionbar的兩種方法
- Android ActionBar使用教程
- 靈活使用Android中ActionBar和ViewPager切換頁面
- Android ActionBar搜索功能用法詳解
- Android巧用ActionBar實(shí)現(xiàn)下拉式導(dǎo)航
- Android編程實(shí)現(xiàn)ActionBar的home圖標(biāo)動畫切換效果
- Android 活動條ActionBar的詳解及實(shí)例代碼
- Android ActionBarActivity設(shè)置全屏無標(biāo)題實(shí)現(xiàn)方法總結(jié)
- Android編程之ActionBar Tabs用法實(shí)例分析
- Android學(xué)習(xí)筆記之ActionBar Item用法分析
相關(guān)文章
Android編程之桌面小部件AppWidgetProvider用法示例
這篇文章主要介紹了Android編程之桌面小部件AppWidgetProvider用法,結(jié)合具體實(shí)例形式分析了Android桌面組件AppWidgetProvider的功能、布局、權(quán)限設(shè)置等相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
Android實(shí)現(xiàn)關(guān)機(jī)重啟的方法分享
這篇文章主要介紹了Android實(shí)現(xiàn)關(guān)機(jī)重啟的方法,需要的朋友可以參考下2014-02-02
Android Flutter基于WebSocket實(shí)現(xiàn)即時通訊功能
WebSocket是一種在單個TCP連接上進(jìn)行全雙工通信的協(xié)議。本文將利用Flutter WebSocket實(shí)現(xiàn)即時通訊功能,文中示例代碼講解詳細(xì),感興趣的可以了解一下2022-03-03
Android獲取雙卡雙待手機(jī)的SIM卡信息示例代碼
這篇文章主要給大家介紹了關(guān)于Android獲取雙卡雙待手機(jī)的SIM卡信息的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11
GridView實(shí)現(xiàn)桌面圖標(biāo)顯示案例
這篇文章主要為大家詳細(xì)介紹了GridView實(shí)現(xiàn)桌面圖標(biāo)顯示案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-08-08
Android shell命令行中過濾adb logcat輸出的方法
本文主要介紹Android shell命令行中過濾adb logcat輸出,這里詳細(xì)說明了shell 命令過濾logcat 輸出內(nèi)容,有需要的小伙伴可以參考下2016-08-08
Android 輕松實(shí)現(xiàn)語音識別詳解及實(shí)例代碼
這篇文章主要介紹了Android 輕松實(shí)現(xiàn)語音識別的相關(guān)資料,并附實(shí)例代碼,需要的朋友可以參考下2016-09-09

