FragmentTabHost的使用

现在市面上app的主流框架大体分为两种:一种是在主界面点击菜单按钮,之后会滑出侧滑菜单,之后进入到各个模块,还有一种是在主界面的下面放置若干个tab按钮,点击按钮,切换到不同的模块。今天要讲的就是第二种的实现方式之一的FragmentTabHost.(选项卡)

一: 首先我们看看XML:

1.activity_main.xml 

<FrameLayout

android:id="@+id/layout_frame"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@color/white" />

<android.support.v4.app.FragmentTabHost

android:id="@android:id/tabhost"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_gravity="bottom">

<FrameLayout

android:id="@android:id/tabcontent"

android:layout_width="0dp"

android:layout_height="0dp" />

</android.support.v4.app.FragmentTabHost>

2.tab.xml

<LinearLayout

android:id="@+id/ll_tab"

android:layout_width="match_parent"

android:layout_height="50dp"

android:background="@color/tab_bg"

android:gravity="center"

android:orientation="vertical">

<ImageView

android:id="@+id/tab_icon"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="5dp"

android:background="@color/tab_bg"

android:src="@mipmap/nav_icon_home_sel" />

<TextView

android:id="@+id/tab_title"

style="@style/CustomTextViewStyle03"

android:layout_marginTop="3dp"

android:text="首页" />

</LinearLayout>

3,Activity中使用如下:

/**

* tabhost选项卡

*/

private TabWidget mTabWidget;

private FragmentTabHost mTabHost;

/**

* 要加载所有的Fragment

*/

private Class<?>[] mFragments = {HomeFragment.class, OrderFragment.class, SelectCarFragment.class, CustomerFragment.class, MineFragment.class,};

private String[] titles = new String[]{"首页", "订单", "底价购车", "客服", "我的"};

mTabHost = findViewByIdNoCast(android.R.id.tabhost);

mTabHost.setup(this, getSupportFragmentManager(), R.id.layout_frame);

//获取一个TabWiidget

mTabWidget = mTabHost.getTabWidget();

mTabWidget.setDividerDrawable(null);

//为tabHost添加fragment

int menuCount = mFragments.length;

for (int i = 0; i < menuCount; i++) {

TabHost.TabSpec spec = mTabHost.newTabSpec(titles[i]).setIndicator(getItemView(i));

mTabHost.addTab(spec, mFragments[i], null);

}

private View getItemView(int index) {

View view = LayoutInflater.from(this).inflate(R.layout.tab, null);

TextView tv = (TextView) view.findViewById(R.id.tab_title);

ImageView icon = (ImageView) view.findViewById(R.id.tab_icon);

return view;

}

//为FragmentTabHost添加点击事件

mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {

@Override

public void onTabChanged(String tabName) {

for (int i = 0; i < mTabWidget.getTabCount(); i++) {

View view = mTabWidget.getChildTabViewAt(i);

TextView tab_title = (TextView) view.findViewById(R.id.tab_title);

ImageView tab_icon = (ImageView) view.findViewById(R.id.tab_icon);

if (i == mTabHost.getCurrentTab()) {

current_tab = i;

tab_icon.setImageResource(iconS[i]);

tab_title.setTextColor(Color.parseColor("#ffff4b14"));

} else {

tab_title.setTextColor(Color.rgb(162, 172, 181));

tab_icon.setImageResource(iconN[i]);

}

}

}

});

//指定tabhost选中那一个

mTabHost.setCurrentTab(0);

//根据tabId获取Fragment

private Fragment getFragment(int tabId)

{

    List<Fragment> fragments = getSupportFragmentManager().getFragments();

    for(Fragment fragment : fragments) {

        String str1 = fragment.getTag();

        String str2 = String.valueOf("TAB_TAG_" + tabId);

        if(str1 != null && str1.equals(str2)) // 最开始没有检查str1是否为空,导致crash!

            return fragment;

    }

    return null;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

休以希

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值