关闭

TabActivity的使用原理与切换卡tabwight的使用

标签: 切换卡TabActivity
740人阅读 评论(0) 收藏 举报
分类:


TabWidget类似Android中查看电话簿的界面,通过多个标签切换显示不同的内容。要实现这一效果,首先要了解TabHost ,它是一个用来存放多个Tab标签的容器。要使用TabHost ,首先需要通过getTabHost 方法来获取TabHost的对象,然后通过addTab方法来向TabHost中添加Tab,废话少说,进入正题:

/*
 * 首先这里新建了一个fesTaHostActivity,以后直接继承该类
 * 只需重写几个方法,protected void prepare()
 * protected int getTabCount(), abstract protected String  getTabItemId(int position);
    abstract protected Intent getTabItemIntent(int position);
     
 */

public abstract class fesTaHostActivity extends TabActivity{
       private TabHost mTabHost;
       private TabWidget mTabWidget;
       private LayoutInflater mLayoutInflater;
       
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setTheme(R.style.Theme_Tabhost);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.api_tab_host);
		mTabHost=getTabHost();//获取TabHost
		mLayoutInflater=getLayoutInflater();
		mTabWidget=getTabWidget();
		
		prepare();
		
		
		initTabSpec();
	}
    
    //初始化tabs
    private void initTabSpec()
    {
    	int count=getTabCount();
    	
    	for(int i=0;i<count;i++)
    	{
    		View tabItem=mLayoutInflater.inflate(R.layout.api_tab_item, null);
    		
    		TextView tvTabItem= (TextView)tabItem.findViewById(R.id.tab_item_tv);//这里是
    		System.out.println("33333"+tvTabItem);
    		setTabItemTextView(tvTabItem, i);
    		
    		String tabItemId=getTabItemId(i);
    		
    		/*
    		 * 添加tab项,
    		 */
    		TabSpec tabSpec=mTabHost.newTabSpec(tabItemId);
    		tabSpec.setIndicator(tabItem);
    		tabSpec.setContent(getTabItemIntent(i));
    		
    		
    		mTabHost.addTab(tabSpec);
    	}
    }

正如上面所说,这是个基类,下面是个继承类,只要重写一些方法,

public class ExampleActivity extends fesTaHostActivity{
    
	List<TabItem> mItems;
	private LayoutInflater mLayoutInflater;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCurrentTab(0);
        //默认加载第一个标签
        
    }
    
    @Override
    protected void prepare()
    {
    	TabItem home=new TabItem("推荐", R.drawable.icon_home,R.drawable.example_tab_item_bg,
                 new Intent(this,Tab1Activity.class)
    			);
    	TabItem festival=new TabItem("节日", R.drawable.icon_square, R.drawable.example_tab_item_bg,
    			new Intent(this,Tab1Activity.class)
    			);
    	TabItem collection=new TabItem("收藏", R.drawable.icon_meassage, R.drawable.example_tab_item_bg,
    			new Intent(this,Tab1Activity.class)
    			);
    	TabItem mine=new TabItem("我", R.drawable.icon_selfinfo, R.drawable.example_tab_item_bg,
    			new Intent(this,Tab1Activity.class)
    			);
    	
    	mItems=new ArrayList<TabItem>();
    	mItems.add(home);
    	mItems.add(festival);
    	mItems.add(collection);
    	mItems.add(mine);
    	
    	TabWidget tabWidget=getTabWidget();
    	tabWidget.setDividerDrawable(R.drawable.tab_divider);
    	
    	mLayoutInflater=getLayoutInflater();
    	
    }
	@Override
	protected void setTabItemTextView(TextView textview, int position) {
		// TODO Auto-generated method stub
		textview.setPadding(3, 3, 3, 3);
		textview.setText(mItems.get(position).getTitle());
		textview.setBackgroundResource(mItems.get(position).getBg());///设置背景
		textview.setCompoundDrawablesWithIntrinsicBounds(0, mItems.get(position).getIcon(), 0, 0);
		//设置Drawable显示在text的左、上、右、下位置
	}

	@Override
	protected String getTabItemId(int position) {
		// TODO Auto-generated method stub
		return mItems.get(position).getTitle();
	}

	@Override
	protected Intent getTabItemIntent(int position) {
		// TODO Auto-generated method stub
		return mItems.get(position).getIntent();
	}
    @Override
	protected int getTabCount()
    {
    	return mItems.size();
    }
    @Override
    protected View getTop()
    {
    	return mLayoutInflater.inflate(R.layout.example_tab_host_top, null);
    }
    
}

不难懂吧,只要重写方法prepare()等方法,就ok了

以下这个是对象tabitem的定义

public class TabItem {
       private String title;//标题
       private int icon;//图片
       private int bg;//背景
       private Intent intent;//跳转下一个界面
       
       public Intent getIntent() {
		return intent;
	}

	public void setIntent(Intent intent) {
		this.intent = intent;
	}

	public TabItem(String title,int icon ,int bg,Intent intent)
       {
    	   super();
    	   this.title=title;
    	   this.icon=icon;
    	   this.bg=bg;
    	   this.intent=intent;
       }

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public int getIcon() {
		return icon;
	}

	public void setIcon(int icon) {
		this.icon = icon;
	}

	public int getBg() {
		return bg;
	}

	public void setBg(int bg) {
		this.bg = bg;
	}

	
       
}

下面是源码下载地址     点击打开链接

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2496次
    • 积分:63
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章存档