Android中怎么使用TextView+LinearLayout实现底部导航栏
Android中怎么使用TextView+LinearLayout实现底部导航栏,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
专注于为中小企业提供成都网站制作、成都做网站服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业乌海海南免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了近1000家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
布局
代码
mTHome.setOnClickListener(this); mTLocation.setOnClickListener(this); mTLike.setOnClickListener(this); mTMe.setOnClickListener(this); setDefaultFragment();//设置默认显示Fragment @Override public void onClick(View view) { resetTabState();//reset the tab state switch (view.getId()) { case R.id.tv_home: setTabState(mTHome, R.drawable.home_fill, getColor(R.color.colorPrimary));//设置Tab状态 switchFrgment(0);//切换Fragment break; case R.id.tv_location: setTabState(mTLocation, R.drawable.location_fill, getColor(R.color.colorPrimary)); switchFrgment(1); break; case R.id.tv_like: setTabState(mTLike, R.drawable.like_fill, getColor(R.color.colorPrimary)); switchFrgment(2); break; case R.id.tv_person: setTabState(mTMe, R.drawable.person_fill, getColor(R.color.colorPrimary)); switchFrgment(3); break; } }
Fragment的切换
/** * switch the fragment accordting to id * @param i id */ private void switchFrgment(int i) { FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); switch (i) { case 0: if (mHomeFragment == null) { mHomeFragment = mHomeFragment.newInstance(getString(R.string.item_home)); } transaction.replace(R.id.sub_content, mHomeFragment); break; case 1: if (mLocationFragment == null) { mLocationFragment = LocationFragment.newInstance(getString(R.string.item_location)); } transaction.replace(R.id.sub_content, mLocationFragment); break; case 2: if (mLikeFragment == null) { mLikeFragment = LikeFragment.newInstance(getString(R.string.item_like)); } transaction.replace(R.id.sub_content, mLikeFragment); break; case 3: if (mPersonFragment == null) { mPersonFragment = PersonFragment.newInstance(getString(R.string.item_person)); } transaction.replace(R.id.sub_content, mPersonFragment); break; } transaction.commit(); }
这里面值得注意的地方就是要用getChildFragmentManager(),否则会出现切换Fragment内容不显示的情况。
设置Tab状态
/** * set the tab state of bottom navigation bar * * @param textView the text to be shown * @param image the image * @param color the text color */ private void setTabState(TextView textView, int image, int color) { textView.setCompoundDrawablesRelativeWithIntrinsicBounds(0, image, 0, 0);//Call requires API level 17 textView.setTextColor(color); } /** * revert the image color and text color to black */ private void resetTabState() { setTabState(mTHome, R.drawable.home, getColor(R.color.black_1)); setTabState(mTLocation, R.drawable.location, getColor(R.color.black_1)); setTabState(mTLike, R.drawable.like, getColor(R.color.black_1)); setTabState(mTMe, R.drawable.person, getColor(R.color.black_1)); }
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。
网页名称:Android中怎么使用TextView+LinearLayout实现底部导航栏
文章出自:http://ybzwz.com/article/pjcpji.html