FragmentTabHost 使用;

4 篇文章 0 订阅
4 篇文章 0 订阅

<pre name="code" class="html"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.dzq.lxq.manager.ui.MainActivity" >

    <FrameLayout
        android:id="@+id/fmlay_container"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" />

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

</LinearLayout>

<pre name="code" class="java">public class MainActivity extends BaseFragmentActivity {
	// 定义FragmentTabHost对象
	private FragmentTabHost mTabHost;
	// 定义数组来存放Fragment界面
	private Class fragmentArray[] = { Main_Home_Fragment.class,
			Main_Data_Fragment.class, Main_Message_Fragment.class,
			Main_My_Fragment.class };

	// 定义数组来存放按钮图片
	private int mImageViewArray[] = { R.drawable.tab_main_bottom_home_btn,
			R.drawable.tab_main_bottom_data_btn,
			R.drawable.tab_main_bottom_message_btn,
			R.drawable.tab_main_bottom_my_btn };

	// Tab选项卡的文字
	private String mTextviewArray[] = { "首页", "数据", "消息", "我的" };

	@Override
	public void setContext() {
		setContentView(R.layout.activity_main);
	}

	@Override
	public void initTopBar() {
		View view = initActionBar(R.layout.common_title_one_txt);
		TextView tv_title = (TextView) view.findViewById(R.id.common_title);
		tv_title.setText("首页");
		TextView common_left_one = (TextView) view
				.findViewById(R.id.common_left_one);
		common_left_one.setText("添加店铺");
		common_left_one.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Fragment mCurrentFragment = getCurrentFragment();
				if (mCurrentFragment instanceof MenuRightBtnOnClick) {
					((MenuRightBtnOnClick) mCurrentFragment).OnClick();
				}
			}
		});
	}

	@Override
	public void findBiyid() {
		// 实例化TabHost对象,得到TabHost
		mTabHost = (FragmentTabHost) findViewById(R.id.tabhost);
		mTabHost.setup(this, getSupportFragmentManager(), R.id.fmlay_container);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
			mTabHost.getTabWidget().setDividerDrawable(R.color.transparent);
		}
		// 得到fragment的个数
		int len = fragmentArray.length;
		for (int i = 0; i < len; i++) {
			// 为每一个Tab按钮设置图标、文字和内容
			TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i])
					.setIndicator(getTabItemView(i));
			// 将Tab按钮添加进Tab选项卡中
			mTabHost.addTab(tabSpec, fragmentArray[i], null);
			// 设置Tab按钮的背景
			mTabHost.getTabWidget().getChildAt(i)
					.setBackgroundResource(R.drawable.common_btn_sel);
		}

	}

	@Override
	public void setListener() {
	}

	@Override
	public void setData() {

	}

	private Fragment getCurrentFragment() {
		return getSupportFragmentManager().findFragmentById(
				R.id.fmlay_container);
	}

	/**
	 * 给Tab按钮设置图标和文字
	 */
	private View getTabItemView(int index) {
		View view = mInflater.inflate(R.layout.main_bottom_tab_item, null);

		// ImageView imageView = (ImageView) view.findViewById(R.id.iv_pic);
		// imageView.setImageResource(mImageViewArray[index]);

		TextView textView = (TextView) view.findViewById(R.id.tv_title);
		textView.setText(mTextviewArray[index]);
		textView.setCompoundDrawablesWithIntrinsicBounds(0,
				mImageViewArray[index], 0, 0);
		return view;
	}

 
最后效果 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值