这里实现的是tabhost类型的菜单选项,还有下拉菜单选项的 http://www.cnblogs.com/hanyuan/archive/2012/04/11/android_actionbar_addingdrop-downnavigation.html

主要过程就是getactionbar 的到actionbar

设置actionbar加载模式

创建tab

创建fragement

给tab加监听tablistener

给actionbar加tab

package com.example.fragement;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {
    FragementA a;
    @SuppressLint("NewApi")
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //得到actionbar
        ActionBar actionbar=getActionBar();
        //设置actionbar的操作模式
        //tab模式
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        //创建tab
        Tab xufeng=actionbar.newTab().setText("徐风");
        Tab chenkai=actionbar.newTab().setText("陈凯");
        // 将Activity的头部去掉
        actionbar.setDisplayShowTitleEnabled(false);
        //实例化Fragement
        a=new FragementA();
        FragementB b=new FragementB();
        //添加监听
        xufeng.setTabListener(new tablistener(a));
        chenkai.setTabListener(new tablistener(b));
        //添加TAB
        actionbar.addTab(xufeng);
        actionbar.addTab(chenkai);
    }
    @SuppressLint("NewApi")
    class tablistener implements TabListener{

        Fragment fg;
        public tablistener(Fragment  a) {
            // TODO Auto-generated constructor stub
            this.fg=a;
        }

        @Override
        public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
            // TODO Auto-generated method stub
            
        }

        @Override
        public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
            // TODO Auto-generated method stub
            //tab 选中时 用对应的Fragement
            arg1.replace(R.id.content, fg);
        }

        @Override
        public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
            // TODO Auto-generated method stub
            arg1.remove(fg);
        }
        
    }
}

 

package com.example.fragement;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

@SuppressLint("NewApi")
public class FragementA extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view=inflater.inflate(R.layout.activity_main, null);
        ((TextView)(view.findViewById(R.id.text))).setText("徐风");
        return view;
    }
    
}

 

posted on 2014-04-04 16:08  青年程序猿  阅读(274)  评论(0编辑  收藏  举报