Android ActionBar詳解(三):ActionBar實現切換Tabs標籤

來源:互聯網
上載者:User

實現切換Tabs標籤;

  

 Activity代碼:  

public class ActionBarTabs extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.action_bar_tabs);}public void onAddTab(View v) {final ActionBar bar = getActionBar();final int tabCount = bar.getTabCount();final String text = "Tab " + tabCount;bar.addTab(bar.newTab().setText(text).setTabListener(new TabListener(new TabContentFragment(text))));}public void onRemoveTab(View v) {final ActionBar bar = getActionBar();bar.removeTabAt(bar.getTabCount() - 1);}public void onToggleTabs(View v) {final ActionBar bar = getActionBar();if (bar.getNavigationMode() == ActionBar.NAVIGATION_MODE_TABS) {bar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_SHOW_TITLE);} else {bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);}}public void onRemoveAllTabs(View v) {getActionBar().removeAllTabs();}private class TabListener implements ActionBar.TabListener {private TabContentFragment mFragment;public TabListener(TabContentFragment fragment) {mFragment = fragment;}public void onTabSelected(Tab tab, FragmentTransaction ft) {ft.add(R.id.fragment_content, mFragment, mFragment.getText());} public void onTabUnselected(Tab tab, FragmentTransaction ft) {ft.remove(mFragment);}public void onTabReselected(Tab tab, FragmentTransaction ft) {Toast.makeText(ActionBarTabs.this, "Reselected!", Toast.LENGTH_SHORT).show();}}private class TabContentFragment extends Fragment {private String mText;public TabContentFragment(String text) {mText = text;}public String getText() {return mText;}  @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View fragView = inflater.inflate(R.layout.action_bar_tab_content, container, false);TextView text = (TextView) fragView.findViewById(R.id.text);text.setText(mText);return fragView;}}}

涉及的布局檔案action_bar_tabs.xml代碼為:

< ?xml version="1.0" encoding="utf-8"?>< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical">< FrameLayout android:id="@+id/fragment_content"android:layout_width="match_parent"android:layout_height="0dip"android:layout_weight="1" />< LinearLayout android:layout_width="match_parent"android:layout_height="0dip"android:layout_weight="1"android:orientation="vertical">< Button android:id="@+id/btn_add_tab"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/btn_add_tab"android:onClick="onAddTab" />< Button android:id="@+id/btn_remove_tab"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/btn_remove_tab"android:onClick="onRemoveTab" />< Button android:id="@+id/btn_toggle_tabs"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/btn_toggle_tabs"android:onClick="onToggleTabs" />< Button android:id="@+id/btn_remove_all_tabs"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/btn_remove_all_tabs"android:onClick="onRemoveAllTabs" />< /LinearLayout>< /LinearLayout>

布局檔案action_bar_tab_content.xml;

< ?xml version="1.0" encoding="utf-8"?>< TextView xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/text"android:layout_width="wrap_content"android:layout_height="wrap_content" />

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.