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
    评论
好的,我可以为您解答。首先,您需要在布局文件中添加一个FragmentTabHost控件,用于显示底部标签栏。然后,您需要在Activity中继承FragmentActivity类,并在onCreate方法中初始化FragmentTabHost控件。最后,您需要创建多个Fragment类,用于显示不同的内容,并通过FragmentTabHost控件将它们与底部标签栏关联起来。 以下是一份示例代码,供您参考: 1. 布局文件 ```xml <android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content"/> </android.support.v4.app.FragmentTabHost> ``` 2. Activity类 ```java public class MainActivity extends FragmentActivity { private FragmentTabHost mTabHost; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTabHost = findViewById(android.R.id.tabhost); mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent); mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab 1"), Tab1Fragment.class, null); mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab 2"), Tab2Fragment.class, null); mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Tab 3"), Tab3Fragment.class, null); } } ``` 3. Fragment类 ```java public class Tab1Fragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_tab1, container, false); } } public class Tab2Fragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_tab2, container, false); } } public class Tab3Fragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_tab3, container, false); } } ``` 请注意,上述代码中的布局文件和Fragment类仅仅是示例,您需要根据自己的需求进行相应的修改。另外,由于使用FragmentTabHost控件,因此需要引入support-v4库。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

休以希

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

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

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

打赏作者

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

抵扣说明:

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

余额充值