实现切换Tabs标签;

Activity代码:

  1. public class ActionBarTabs extends Activity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.action_bar_tabs);
  6. }
  7. public void onAddTab(View v) {
  8. final ActionBar bar = getActionBar();
  9. final int tabCount = bar.getTabCount();
  10. final String text = "Tab " + tabCount;
  11. bar.addTab(bar.newTab().setText(text)
  12. .setTabListener(new TabListener(new TabContentFragment(text))));
  13. }
  14. public void onRemoveTab(View v) {
  15. final ActionBar bar = getActionBar();
  16. bar.removeTabAt(bar.getTabCount() - 1);
  17. }
  18. public void onToggleTabs(View v) {
  19. final ActionBar bar = getActionBar();
  20. if (bar.getNavigationMode() == ActionBar.NAVIGATION_MODE_TABS) {
  21. bar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
  22. bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_SHOW_TITLE);
  23. } else {
  24. bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  25. bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
  26. }
  27. }
  28. public void onRemoveAllTabs(View v) {
  29. getActionBar().removeAllTabs();
  30. }
  31. private class TabListener implements ActionBar.TabListener {
  32. private TabContentFragment mFragment;
  33. public TabListener(TabContentFragment fragment) {
  34. mFragment = fragment;
  35. }
  36. public void onTabSelected(Tab tab, FragmentTransaction ft) {
  37. ft.add(R.id.fragment_content, mFragment, mFragment.getText());
  38. }
  39. public void onTabUnselected(Tab tab, FragmentTransaction ft) {
  40. ft.remove(mFragment);
  41. }
  42. public void onTabReselected(Tab tab, FragmentTransaction ft) {
  43. Toast.makeText(ActionBarTabs.this, "Reselected!", Toast.LENGTH_SHORT).show();
  44. }
  45. }
  46. private class TabContentFragment extends Fragment {
  47. private String mText;
  48. public TabContentFragment(String text) {
  49. mText = text;
  50. }
  51. public String getText() {
  52. return mText;
  53. }
  54.   
  55. @Override
  56. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  57. Bundle savedInstanceState) {
  58. View fragView = inflater.inflate(R.layout.action_bar_tab_content, container, false);
  59. TextView text = (TextView) fragView.findViewById(R.id.text);
  60. text.setText(mText);
  61. return fragView;
  62. }
  63. }
  64. }

涉及的布局文件action_bar_tabs.xml代码为:

  1. < ?xml version="1.0" encoding="utf-8"?>
  2. < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6. < FrameLayout android:id="@+id/fragment_content"
  7. android:layout_width="match_parent"
  8. android:layout_height="0dip"
  9. android:layout_weight="1" />
  10. < LinearLayout android:layout_width="match_parent"
  11. android:layout_height="0dip"
  12. android:layout_weight="1"
  13. android:orientation="vertical">
  14. < Button android:id="@+id/btn_add_tab"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:text="@string/btn_add_tab"
  18. android:onClick="onAddTab" />
  19. < Button android:id="@+id/btn_remove_tab"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:text="@string/btn_remove_tab"
  23. android:onClick="onRemoveTab" />
  24. < Button android:id="@+id/btn_toggle_tabs"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:text="@string/btn_toggle_tabs"
  28. android:onClick="onToggleTabs" />
  29. < Button android:id="@+id/btn_remove_all_tabs"
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:text="@string/btn_remove_all_tabs"
  33. android:onClick="onRemoveAllTabs" />
  34. < /LinearLayout>
  35. < /LinearLayout>

布局文件action_bar_tab_content.xml;

    1. < ?xml version="1.0" encoding="utf-8"?>
    2. < TextView xmlns:android="http://schemas.android.com/apk/res/android"
    3. android:id="@+id/text"
    4. android:layout_width="wrap_content"
    5. android:layout_height="wrap_content" />
【JAVA】鉴于plaincopy

Android ActionBar详解(三):ActionBar实现切换Tabs标签的更多相关文章

  1. Android ActionBar详解(三)--->ActionBar的Home导航功能

    FirstActivity如下: package cc.testsimpleactionbar2; import android.os.Bundle; import android.app.Activ ...

  2. (转)android Fragments详解三:实现Fragment的界面

    为fragment添加用户界面 fragment一般作为activity的用户界面的一部分,把它自己的layout嵌入到activity的layout中.    一个 要为fragment提供layo ...

  3. 【转】Android编译系统详解(三)——编译流程详解

    原文网址:http://www.cloudchou.com/android/post-276.html 本文原创作者:Cloud Chou. 欢迎转载,请注明出处和本文链接 1.概述 编译Androi ...

  4. Android Fragment详解(三): 实现Fragment的界面

    为fragment添加用户界面: Fragment一般作为activity的用户界面的一部分,把它自己的layout嵌入到activity的layout中. 一个 要为fragment提供layout ...

  5. Android 布局详解 -三表格布局(TableLayout)以及重要属性

              TableLayout跟TableRow 是一组搭配应用的布局,TableLayout置底,TableRow在TableLayout的上方,而Button.TextView等控件就 ...

  6. Android Loader详解三:重启与回调

    重启装载器 当你使用initLoader()时,如果指定ID的装载器已经存在,则它使用这个装载器.如果不存在呢,它将创建一个新的.但是有时你却是想丢弃旧的然后开始新的数据. 要想丢弃旧数据,你应使用r ...

  7. Android ActionBar详解

    Android ActionBar详解 分类: Android2014-04-30 15:23 1094人阅读 评论(0) 收藏 举报 androidActionBar   目录(?)[+]   第4 ...

  8. Android 之窗口小部件详解(三)  部分转载

    原文地址:http://blog.csdn.net/iefreer/article/details/4626274. (一) 应用程序窗口小部件App Widgets 应用程序窗口小部件(Widget ...

  9. Android Notification 详解(一)——基本操作

    Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...

随机推荐

  1. 设计模式14---设计模式之命令模式(Command)(行为型)

    1.场景模拟 请用软件模拟开机过程 按下启动按钮 然后电源供电 主板开始加电自检 BIOS依次寻找其他设备的BIOS并且让他们初始化自检 开始检测CPU,内存,光盘,硬盘,光驱,串口,并口,软驱即插即 ...

  2. Python-字符串开头或结尾匹配

    startswith() 和 endswith() 方法提供了一个非常方便的方式去做字符串开头和结尾的检查. 1.查看指定目录下的所有文件名 >>> import os >&g ...

  3. SVN 代码下载,上传

    代码下载,如: svn co https://99.99.16.1:8080/svn/pavenas/webpy --username bg 代码上传,如: svn commit -m "备 ...

  4. django: db howto - 1

    以在 Django 中使用 MySQL 为例,首先要安装 MySQL 和 MySQL-python 组件,确保 python 能执行 import MySQLdb. MySQL 中创建数据库: [ro ...

  5. js判断值是否为数字

    js判断是否是数字 第一种方法 isNaN isNaN 返回一个 Boolean 值,指明提供的值是否是保留值 NaN (不是数字). NaN 即 Not a Number isNaN(numValu ...

  6. Webpack: 为Web开发而生的模块管理器[转]

    Webpack: 为Web开发而生的模块管理器 原文地址:http://hanjianwei.com/2014/09/10/webpack-package-manager-for-web/ 10 Se ...

  7. 绘制数据图表的又一利器:C3.js

  8. C程序设计语言练习题1-11

    练习1-11 你准备如何测试单词计数程序?如果程序中存在某种错误,那么什么样的输入最可能发现这类错误呢? 代码如下: #include <stdio.h> // 包含标准库的信息. #de ...

  9. DataTables自定义事件

    $(document).ready(function() { var eventFired = function(type) { var n = $('#demo_info')[0]; n.inner ...

  10. Android从相册读取图片

    Uri originalUri = data.getData();        //获得图片的uri  bm = MediaStore.Images.Media.getBitmap(resolver ...