Android ActionBar详解(三):ActionBar实现切换Tabs标签
实现切换Tabs标签;
Activity代码:
- public class ActionBarTabs extends Activity {
- @Override
- protected 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;
- }
- @Override
- public 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" />
Android ActionBar详解(三):ActionBar实现切换Tabs标签的更多相关文章
- Android ActionBar详解(三)--->ActionBar的Home导航功能
FirstActivity如下: package cc.testsimpleactionbar2; import android.os.Bundle; import android.app.Activ ...
- (转)android Fragments详解三:实现Fragment的界面
为fragment添加用户界面 fragment一般作为activity的用户界面的一部分,把它自己的layout嵌入到activity的layout中. 一个 要为fragment提供layo ...
- 【转】Android编译系统详解(三)——编译流程详解
原文网址:http://www.cloudchou.com/android/post-276.html 本文原创作者:Cloud Chou. 欢迎转载,请注明出处和本文链接 1.概述 编译Androi ...
- Android Fragment详解(三): 实现Fragment的界面
为fragment添加用户界面: Fragment一般作为activity的用户界面的一部分,把它自己的layout嵌入到activity的layout中. 一个 要为fragment提供layout ...
- Android 布局详解 -三表格布局(TableLayout)以及重要属性
TableLayout跟TableRow 是一组搭配应用的布局,TableLayout置底,TableRow在TableLayout的上方,而Button.TextView等控件就 ...
- Android Loader详解三:重启与回调
重启装载器 当你使用initLoader()时,如果指定ID的装载器已经存在,则它使用这个装载器.如果不存在呢,它将创建一个新的.但是有时你却是想丢弃旧的然后开始新的数据. 要想丢弃旧数据,你应使用r ...
- Android ActionBar详解
Android ActionBar详解 分类: Android2014-04-30 15:23 1094人阅读 评论(0) 收藏 举报 androidActionBar 目录(?)[+] 第4 ...
- Android 之窗口小部件详解(三) 部分转载
原文地址:http://blog.csdn.net/iefreer/article/details/4626274. (一) 应用程序窗口小部件App Widgets 应用程序窗口小部件(Widget ...
- Android Notification 详解(一)——基本操作
Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...
随机推荐
- Jenkins学习之——(1)Jenkins的安装与配置
1.最近公司要求做自动化部署,于是自学了jenkins.这个参考书很少,网上的文章也讲得很模糊,于是打算把自己学习东西记下来,希望对大家有所帮助. 一.jenkins的安装 到jenkins官网(ht ...
- POJ3104 Drying(二分查找)
POJ3104 Drying 这个题由于题目数据比较大(1 ≤ ai ≤ 109),采用贪心的话肯定会超时,自然就会想到用二分. 设C(x)为true时表示所用时间为X时,可以把所有的衣服都烘干或者自 ...
- uva 846 - Steps
找出步數與距離的關係即可得解. 0步最多能抵達的距離是0 1步最多能抵達的距離是1(1) 2步最多能抵達的距離是2(1 1) 3步最多能抵達的距離是4(1 2 1) 4步最多能抵達的距離是6(1 2 ...
- web标准(复习)--2 列布局
今天我们开始学习一列布局,包含以下几种形式: 1.一列固定宽度 2.一列固定宽度居中 3.一列自适应宽度 4.一列自适应宽度居中 5.一列二至多块布局 前一节我们回顾了xhtml基础和css基础部分, ...
- CSAPP--虚拟存储器
虚拟存储器 虚拟存储器(VM)是对主存的一种抽象概念.是硬件一场,硬件地址翻译,贮存,磁盘文件和内核软件的完美交互.他为每个进程提供了一个大的,一致的和私有的地址空间. 它将贮存堪称一个存储在磁盘上的 ...
- sqlachemy 使用实例
sqlachemy 是python中关于sql的ORM,他的存在可以消除底层sql引擎的差异,同事也避免了复杂繁琐的sql语句,因此我们在比较大的应用时常使用它,下面是我写的一个例子 #!/usr/b ...
- My advice to young people - Donald Knuth [video]
http://www.youtube.com/watch?v=75Ju0eM5T2c I took a note of what knuth said in the video. 1. Don't d ...
- ServletConfig对象 【通过此对象获取到web.xml中的信息】
用途: 1)想让当前的Servlet读取一些在web.xml文件配置的初始化参数时, 可以使用ServletConfig对象,他是Servlet运 ...
- 将某个Qt4项目升级到Qt5遇到的问题(13条方法)
本文转载自http://hi.baidu.com/xchinux/item/9044d8ce986accbb0d0a7b87 一.将某个QT4项目改成QT5遇到的问题 该Qt4项目以前是使用Qt4.7 ...
- 购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(3)--Idetity,OWIN前后端验证
原文:购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(3)--Idetity,OWIN前后端验证 chsakell分享了前端使用AngularJS,后端使用ASP. ...