ViewPage,TabHost与Fragment(上)

一.TabHost

//源码地址在底部

       0x001.布局文件

      

<?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/contentLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </FrameLayout>

    <android.support.v4.app.FragmentTabHost
        android:id="@+id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FF8201" >

        <FrameLayout
            android:id="@+id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp" >
        </FrameLayout>
    </android.support.v4.app.FragmentTabHost>

</LinearLayout>

        使用android.support.v4包里面的FragmentTabHost控件

       0x002.主要代码

      创建一个TabHost的基类,方便其他类的调用

package util.l.viewpage;

import java.util.List;

import util.l.viewpage.util.Tab;
import util.l.viewpage.util.TabDb;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;
import android.widget.TextView;

public class BaseTabHost extends FragmentActivity implements
		OnTabChangeListener {
	private FragmentTabHost tabHost;
	private int mColor = Color.RED;
	private int mBColor = Color.RED;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.tab_host_activity);
		tabHost = (FragmentTabHost) findViewById(R.id.tabhost);
		tabHost.setup(this, super.getSupportFragmentManager(),
				R.id.contentLayout);
		tabHost.getTabWidget().setDividerDrawable(null);
		tabHost.setOnTabChangedListener(this);
		initTab();
	}

	private void initTab() {
		List<String> tabs = TabDb.getMtabs();
		for (int i = 0; i < tabs.size(); i++) {
			TabSpec tabSpec = tabHost.newTabSpec(tabs.get(i)).setIndicator(
					getTabView(i));
			tabHost.addTab(tabSpec, TabDb.getMcls().get(i), null);
			tabHost.setTag(i);
		}
	}

	private View getTabView(int idx) {
		View view = LayoutInflater.from(this).inflate(R.layout.footer_tabs,
				null);
		view.setBackgroundColor(mBColor);
		((TextView) view.findViewById(R.id.tvTab))
				.setText(TabDb.getMtabs().get(idx));
		if (idx == 0) {
			((TextView) view.findViewById(R.id.tvTab)).setTextColor(mColor);
			((ImageView) view.findViewById(R.id.ivImg)).setImageResource(TabDb
					.getMimgs_light().get(idx));
		} else {
			((ImageView) view.findViewById(R.id.ivImg)).setImageResource(TabDb
					.getMimgs().get(idx));
		}
		return view;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onTabChanged(String arg0) {
		updateTab();
	}

	private void updateTab() {
		TabWidget tabw = tabHost.getTabWidget();
		for (int i = 0; i < tabw.getChildCount(); i++) {
			View view = tabw.getChildAt(i);
			ImageView iv = (ImageView) view.findViewById(R.id.ivImg);
			if (i == tabHost.getCurrentTab()) {
				((TextView) view.findViewById(R.id.tvTab))
						.setTextColor(mColor);
				iv.setImageResource(TabDb.getMimgs_light().get(i));
			} else {
				((TextView) view.findViewById(R.id.tvTab))
						.setTextColor(getResources().getColor(
								android.R.color.darker_gray));
				iv.setImageResource(TabDb.getMimgs().get(i));
			}
		}
	}
	
	/**
	 * setTextColor 修改底部Tab栏的字体颜色
	 * @param Color 底部tab栏被选中项文字的颜色		Default Color.RED		
	 */
	
	public void setTextColor(int Color){
		mColor = Color;
	}
	
	/**
	 * setTabHostColor 修改底部Tab栏的背景颜色
	 * @param BColor 需要设置的背景颜色	Default Color.RED
	 */
	public void setTabHostColor(int BColor){
		mBColor = BColor;
	}
	
	/**
	 * setItems	设置Item页
	 * @param tabs	需要显示的Tab
	 */
	public void setItems(List<Tab> tabs) {
		TabDb.setTabs(tabs);
	}
	/**
	 * addTab	添加Tab页面
	 * @param tab	需要添加的Tab
	 */
	public void addTab(Tab tab){
		TabDb.addTab(tab);
		tabHost.clearAllTabs();
		initTab();
	}
	/**
	 * removeTabById	通过Id来删除所对应的Tab页面
	 * @param id		需要删除的Tab页面的Id
	 */
	public void removeItemById(int id){
		TabDb.removeTab(id);
		tabHost.clearAllTabs();
		initTab();
	}
	
	@Deprecated
	public void removeItemByName(){
		
	}
}

         0x003.子类调用

           在OnCreate之前初始化需要填充的数据,就可已用了

package util.l.viewpage;

import java.util.ArrayList;
import java.util.List;

import util.l.viewpage.Fragment.TabFragment1;
import util.l.viewpage.Fragment.TabFragment2;
import util.l.viewpage.util.Tab;
import android.R;
import android.graphics.Color;
import android.os.Bundle;

public class TabHostActivity extends BaseTabHost {
	@Override
	protected void onCreate(Bundle arg0) {
		init();
		super.onCreate(arg0);
		setTabHostColor(Color.BLUE);
		setTextColor(Color.GREEN);
		addView();
	}

	private void init() {
		String[] tabsText = {"新闻","阅读","视听","发现","我"};
		int[] imgs = { R.drawable.checkbox_off_background,
				R.drawable.checkbox_off_background,
				R.drawable.checkbox_off_background,
				R.drawable.checkbox_off_background,
				R.drawable.checkbox_off_background };
		int[] imgs_light =  { R.drawable.checkbox_on_background,
				R.drawable.checkbox_on_background,
				R.drawable.checkbox_on_background,
				R.drawable.checkbox_on_background,
				R.drawable.checkbox_on_background };
		Class[] cls = { TabFragment1.class, TabFragment2.class,
				TabFragment1.class, TabFragment2.class, TabFragment1.class };
		List<Tab> tabs = new ArrayList<Tab>();
		for (int i = 0; i < cls.length; i++) {
			Tab tab = new Tab(tabsText[i], imgs[i], imgs_light[i], cls[i]);
			tabs.add(tab);
		}
		setItems(tabs);
	}
	
	private void addView() {
		Tab tab = new Tab("add", R.drawable.checkbox_off_background, R.drawable.checkbox_on_background, TabFragment2.class);
		addTab(tab);
	}
	
}


    源码下载: http://download.csdn.net/detail/xiao_wl/9478693
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值