Android巧用ActionBar實(shí)現(xiàn)tab導(dǎo)航效果
本文實(shí)例為大家分享了ActionBar實(shí)現(xiàn)tab導(dǎo)航效果的具體代碼,供大家參考,具體內(nèi)容如下
先來(lái)說(shuō)一說(shuō)基礎(chǔ)知識(shí):
一、基本使用方法
1.獲取ActionBar
獲取actionbar很簡(jiǎn)單,在activity中,ationbar=this.getActionBar();
2.顯示/隱藏
設(shè)置actionBar的顯示/隱藏,可以使用show()和hide()方法。
3.設(shè)置MenuItem
通過(guò)設(shè)置menuItem,可以使menuItem成為actionbar上的item。
setShowAsAction(int actionenum),這個(gè)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位置足夠時(shí)才顯示在actionbar上。
SHOW_AS_ACTION_NEVER:不將該menuitem顯示在actionbar上。
SHOW_AS_ACTION_WITH_TEXT:把menuItem顯示在actionbar上,并且顯示該菜單項(xiàng)的文本。
也可以在xml屬性,定義item的屬性 android:showAsAction來(lái)設(shè)置。
4.啟用程序圖標(biāo)導(dǎo)航
setDisplayHomeAsUpEnabled(boolean showHomeAsUp):設(shè)置是否應(yīng)該將應(yīng)用程序圖標(biāo)轉(zhuǎn)變成可點(diǎn)擊的圖標(biāo),并且在圖標(biāo)上添加一個(gè)向左的箭頭。
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時(shí),使用android:actionViewClass屬性指定Action View的實(shí)現(xiàn)類(lèi)。
定義Action Item時(shí),使用android:actionLayout屬性指定Action View對(duì)應(yīng)的視圖資源。
二、Android巧用ActionBar實(shí)現(xiàn)tab導(dǎo)航效果
利用actionbar同樣也可以輕松的實(shí)現(xiàn)tab導(dǎo)航的效果,配合使用fragment實(shí)現(xiàn)切換不同view的功能。
若想使用這個(gè)功能,需要
1)設(shè)置actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS),使actionbar使用tab導(dǎo)航功能。
2)調(diào)用actionbar的addTab()方法,添加多個(gè)tab標(biāo)簽,并為每個(gè)tab標(biāo)簽添加時(shí)間監(jiān)聽(tīng)器。
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)效果:


以上就是本文的全部?jī)?nèi)容,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Android自定義ActionBar實(shí)例
- Android中ActionBar以及menu的代碼設(shè)置樣式
- android中開(kāi)啟actionbar的兩種方法
- Android ActionBar使用教程
- 靈活使用Android中ActionBar和ViewPager切換頁(yè)面
- Android ActionBar搜索功能用法詳解
- Android巧用ActionBar實(shí)現(xiàn)下拉式導(dǎo)航
- Android編程實(shí)現(xiàn)ActionBar的home圖標(biāo)動(dòng)畫(huà)切換效果
- Android 活動(dòng)條ActionBar的詳解及實(shí)例代碼
- Android ActionBarActivity設(shè)置全屏無(wú)標(biāo)題實(shí)現(xiàn)方法總結(jié)
- Android編程之ActionBar Tabs用法實(shí)例分析
- Android學(xué)習(xí)筆記之ActionBar Item用法分析
相關(guān)文章
android開(kāi)發(fā)教程之卸載sd卡對(duì)MediaServer的處理
Android中如果MediaServer訪問(wèn)SD卡上的音頻文件,卸載SD卡的時(shí)候,就會(huì)kill掉MediaServer,卸載SD卡上必要條件就是沒(méi)有進(jìn)程訪問(wèn)SD卡上的資源文件。Kill掉MediaServer的進(jìn)程后,MediaServer會(huì)重新啟動(dòng)。2014-02-02
Android自定義View實(shí)現(xiàn)字母導(dǎo)航欄的代碼
這篇文章主要介紹了Android自定義View實(shí)現(xiàn)字母導(dǎo)航欄的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
Android實(shí)現(xiàn)直播聊天區(qū)域中頂部的漸變效果
最近在研究直播的彈幕,東西有點(diǎn)多,準(zhǔn)備記錄一下免得自己忘了又要重新研究,下面這篇文章主要給大家介紹了關(guān)于Android如何實(shí)現(xiàn)直播聊天區(qū)域中頂部漸變效果的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2018-04-04
Android程序開(kāi)發(fā)如何處理圖像格式類(lèi)及圖像轉(zhuǎn)換
這篇文章主要介紹了Android程序開(kāi)發(fā)如何處理圖像格式類(lèi)及圖像轉(zhuǎn)換,需要的朋友可以參考下2015-07-07
android實(shí)現(xiàn)藍(lán)牙app代碼
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)藍(lán)牙app的代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Android自定義RadioGroupX實(shí)現(xiàn)多行多列布局
這篇文章主要為大家詳細(xì)介紹了Android自定義RadioGroupX實(shí)現(xiàn)多行多列布局,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
Android創(chuàng)建與解析XML(二)——詳解Dom方式
本篇文章主要介紹了Android創(chuàng)建與解析XML(二)——詳解Dom方式 ,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2016-11-11
Android實(shí)現(xiàn)簡(jiǎn)單QQ登錄頁(yè)面
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)單QQ登錄頁(yè)面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04

