你好,游客 登录 注册 搜索
背景:
阅读新闻

Android之Actionbar顶部标签的使用

[日期:2014-08-04] 来源:Linux社区  作者:xuzhiwei [字体:大 中 小]

今天写了个示例代码,就是使用Actionbar类实现顶部标签切换功能。如下图所示。

Android之Actionbar顶部标签的使用

使用最新的adt工具,创建项目的时候都会带一个Android-support-v7-appcompat的类库项目,这个libproject中有我们要用的ActionBar,可以适配2.1的Android系统。

本文示例代码下载 

------------------------------------------分割线------------------------------------------

免费下载地址在 http://linux.linuxidc.com/

用户名与密码都是www.linuxidc.com

具体下载目录在 /2014年资料/8月/4日/Android之Actionbar顶部标签的使用

下载方法见 http://www.linuxidc.com/Linux/2013-07/87684.htm

------------------------------------------分割线------------------------------------------

Ubuntu 14.04 x64配置Android 4.4 kitkat编译环境的方法 http://www.linuxidc.com/Linux/2014-05/101148.htm

Ubuntu 12.04搭建Android开发环境 http://www.linuxidc.com/Linux/2012-09/69961.htm

Ubuntu 14.04 配置 Android SDK 开发环境 http://www.linuxidc.com/Linux/2014-05/101039.htm

64位Ubuntu 11.10下Android开发环境的搭建(JDK+Eclipse+ADT+Android SDK详细) http://www.linuxidc.com/Linux/2013-06/85303.htm

Ubuntu 12.10 x64 安装 Android SDK http://www.linuxidc.com/Linux/2013-03/82005.htm

废话不多说,直接上代码。

1、修改activity_main.xml,增加ViewPager。

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

2、修改MainActivity中的代码,让其继承ActionBarActivity

public class MainActivity extends ActionBarActivity implements TabListener {

3、创建TabsPagerAdapter继承FragmentPagerAdapter

package com.example.tabswithswie.adatper;
 
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
 
import com.example.tabswithswie.fragments.AppFragment;
import com.example.tabswithswie.fragments.GamesFragment;
import com.example.tabswithswie.fragments.MoviesFragment;
 
public class TabsPagerAdapter extends FragmentPagerAdapter {
 
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
 
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new AppFragment();
case 1:
return new GamesFragment();
case 2:
return new MoviesFragment();
 
}
return null;
}
 
@Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
 
}

4、创建AppFragment继承android.support.v4.app.Fragment

package com.example.tabswithswie.fragments;
 
import com.example.tabswithswie.R;
 
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
 
public class AppFragment extends Fragment {
 
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment_app, container, false);
}
}

5、创建布局文件fragment_app.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#43ff00ff" >
 
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="这个是应用界面"
android:textAppearance="?android:attr/textAppearanceLarge" />
 
</RelativeLayout>

6、创建GamesFragment继承android.support.v4.app.Fragment

package com.example.tabswithswie.fragments;
 
import com.example.tabswithswie.R;
 
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
 
public class GamesFragment extends Fragment {
 
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment_game, container, false);
}
}

更多详情见请继续阅读下一页的精彩内容: http://www.linuxidc.com/Linux/2014-08/105108p2.htm

linux
相关资讯       Android ActionBar  Android标签  Actionbar使用 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款