您好,登錄后才能下訂單哦!
本文實例講述了Android編程之ActionBar Tabs用法。分享給大家供大家參考,具體如下:
這里主要實現用Tab切換不同的Fragment,點擊View顯示or隱藏ActionBar,把ActionBar 設為透明,使界面更加友好,詳細代碼見資源里的ActionBarTabs。
ActionBar Tab主要用于Fragment之間的切換,其必須要設置ActionBar.TabListener,詳細代碼如下
ActionBarActivity.Java:
import android.app.ActionBar; import android.app.Activity; import android.app.FragmentTransaction; import android.app.ActionBar.Tab; import android.os.Bundle; import android.os.CountDownTimer; import android.view.MotionEvent; import android.view.Window; public class ActionBarActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //使ActionBar變得透明 requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY); setContentView(R.layout.main); final ActionBar actionBar = getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); // remove the activity title to make space for tabs actionBar.setDisplayShowTitleEnabled(false); AFragment aFragment = new AFragment(); actionBar.addTab(actionBar.newTab().setText("Tab-A") .setTabListener(new ListenerA(aFragment))); BFragment bFragment = new BFragment(); actionBar.addTab(actionBar.newTab().setText("Tab-B") .setTabListener(new ListenerB(bFragment))); } //點擊顯示or隱藏ActionBar public boolean onTouchEvent(MotionEvent event){ ActionBar bar = getActionBar(); switch(event.getAction()){ case MotionEvent.ACTION_UP: if(bar.isShowing()) bar.hide(); else bar.show(); break; default: break; } return true; } private class ListenerA implements ActionBar.TabListener { private AFragment mFragment; // Called to create an instance of the listener when adding a new tab public ListenerA(AFragment fragment) { mFragment = fragment; } public void onTabSelected(Tab tab, FragmentTransaction ft) { ft.add(R.id.fragment, mFragment, null); } public void onTabUnselected(Tab tab, FragmentTransaction ft) { ft.remove(mFragment); } public void onTabReselected(Tab tab, FragmentTransaction ft) { // do nothing } } } private class ListenerB implements ActionBar.TabListener { private BFragment mFragment; // Called to create an instance of the listener when adding a new tab public ListenerB(BFragment fragment) { mFragment = fragment; } public void onTabSelected(Tab tab, FragmentTransaction ft) { ft.add(R.id.fragment, mFragment, null); } public void onTabUnselected(Tab tab, FragmentTransaction ft) { ft.remove(mFragment); } public void onTabReselected(Tab tab, FragmentTransaction ft) { // do nothing } } } }
其中涉及到兩個Fragment,在前面Fragment的筆記中講過,這里就不再贅述。類AFragment實現如下,BFragment實現與這類似:
public class AFragment extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.alayout, container, false); } }
更多關于Android相關內容感興趣的讀者可查看本站專題:《Android開發入門與進階教程》、《Android調試技巧與常見問題解決方法匯總》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。