这里简单记录下通过TextView+LinearLayout+Fragment来实现Android底部导航栏。

布局

<?xml version="1.0" encoding="utf-8"?>                        <?xml version="1.0" encoding="utf-8"?>                

代码

    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));    }

说明:这几篇文章没有过多的文字叙述,因为这些东西也不是很难,而且都是常用的,相信很多人都了如指掌了,多说亦是废话,直接上代码看的反而更清楚。

DownLoad Demo

更多相关文章

  1. android permission
  2. Android(安卓)WebView相关属性
  3. Android(安卓)Studio动态调试Smali
  4. 一对一直播系统开发Android(安卓)ProgressBar圆形进度条颜色设置
  5. Android(安卓)如何调用MediaPlayer播放视频[状态机]【图】
  6. android studio Build 设置简介
  7. android画图-解锁界面
  8. android 一键锁屏实现
  9. Android(安卓)TabLayout导航条属性的设置

随机推荐

  1. 自动化收集SQLSERVER诊断信息的工具选择
  2. SQLSERVER聚集索引和主键(Primary Key)的
  3. SQLserver删除某数据库中所有表实现思路
  4. sqlserver 各种判断是否存在(表名、函数
  5. SqlServer系统数据库的作用深入了解
  6. linux下apache、mysql、php安装配置详细
  7. SQL Server代理服务无法启动的解决方法
  8. SQL Server中减小Log文件尺寸的方法分享
  9. 自动清理 MSSQL Server Table Collation
  10. sql server中查找特定类别的列的写法