美文网首页Android开发Android开发经验谈Android开发
android创建类似iOS的tabbar如何实现

android创建类似iOS的tabbar如何实现

作者: Matsonga | 来源:发表于2019-05-27 23:50 被阅读22次

不知道怎么在drawable里建item的自行百度

先看目录结构:


目录结构.png

MainActivity

package com.example.matsonga.tabbaractivity;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    //初始化fragment
    private HomePageFragment      mHomePageFragment;
    private MatchPageFragment     mMatchPageFragment;
    private RecommendPageFragment mRecommendPageFragment;
    private MinePageFragment      mMinePageFragment;

    //片段类容
    private FrameLayout mFlFragmentContent;
    //底部4个按钮
    private RelativeLayout mRlFirstLayout;
    private RelativeLayout mRlSecondLayout;
    private RelativeLayout mRlThirdLayout;
    private RelativeLayout mRlFourLayout;

    private ImageView mIvFirstHome;
    private TextView  mTvFirstHome;
    private ImageView mIvSecondMatch;
    private TextView  mTvSecondMatch;
    private ImageView mIvThirdRecommend;
    private TextView  mTvThirdRecommend;
    private ImageView mIvFourMine;
    private TextView  mTvFourMine;
    private FragmentManager     mFragmentManager;
    private FragmentTransaction mTransaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mFragmentManager = getSupportFragmentManager();
        initView();     //初始化视图

    }

//    //此方法可以让app启动页像微信一样,第一次(启动页运行),第二次(无启动页,直接进入主界面)
//    @Override
//    public void onBackPressed() {
//        //super.onBackPressed();    不要调用父类
//        Intent intent = new Intent(Intent.ACTION_MAIN);     //ACTION_MAIN主活动
//        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);     //标志活动新任务
//        intent.addCategory(Intent.CATEGORY_HOME);           //类型
//        startActivity(intent);
//    }

    private void initView() {
        mFlFragmentContent = (FrameLayout)    findViewById(R.id.fl_fragment_content);
        mRlFirstLayout     = (RelativeLayout) findViewById(R.id.rl_first_layout);
        mIvFirstHome       = (ImageView)      findViewById(R.id.iv_first_home);
        mTvFirstHome       = (TextView)       findViewById(R.id.tv_first_home);
        mRlSecondLayout    = (RelativeLayout) findViewById(R.id.rl_second_layout);
        mIvSecondMatch     = (ImageView)      findViewById(R.id.iv_second_match);
        mTvSecondMatch     = (TextView)       findViewById(R.id.tv_second_match);
        mRlThirdLayout     = (RelativeLayout) findViewById(R.id.rl_third_layout);
        mIvThirdRecommend  = (ImageView)      findViewById(R.id.iv_third_recommend);
        mTvThirdRecommend  = (TextView)       findViewById(R.id.tv_third_recommend);
        mRlFourLayout      = (RelativeLayout) findViewById(R.id.rl_four_layout);
        mIvFourMine        = (ImageView)      findViewById(R.id.iv_four_mine);
        mTvFourMine        = (TextView)       findViewById(R.id.tv_four_mine);
        //给五个按钮设置监听器
        mRlFirstLayout. setOnClickListener(this);
        mRlSecondLayout.setOnClickListener(this);
        mRlThirdLayout. setOnClickListener(this);
        mRlFourLayout.  setOnClickListener(this);
        //默认第一个首页被选中高亮显示
        mRlFirstLayout. setSelected(true);
        mTransaction = mFragmentManager.beginTransaction();
        mTransaction.replace(R.id.fl_fragment_content, new HomePageFragment());
        mTransaction.commit();
    }

    @Override
    public void onClick(View v) {
        mTransaction = mFragmentManager.beginTransaction(); //开启事务
        hideAllFragment(mTransaction);
        switch (v.getId()){
            //首页
            case R.id.rl_first_layout:
                seleted();
                mRlFirstLayout.setSelected(true);
                if (mHomePageFragment == null) {
                    mHomePageFragment = new HomePageFragment();
                    mTransaction.add(R.id.fl_fragment_content,mHomePageFragment);    //通过事务将内容添加到内容页
                }else{
                    mTransaction.show(mHomePageFragment);
                }
                break;
            //赛程
            case R.id.rl_second_layout:
                seleted();
                mRlSecondLayout.setSelected(true);
                if (mMatchPageFragment == null) {
                    mMatchPageFragment = new MatchPageFragment();
                    mTransaction.add(R.id.fl_fragment_content,mMatchPageFragment);    //通过事务将内容添加到内容页
                }else{
                    mTransaction.show(mMatchPageFragment);
                }
                break;
            //推荐
            case R.id.rl_third_layout:
                seleted();
                mRlThirdLayout.setSelected(true);
                if (mRecommendPageFragment == null) {
                    mRecommendPageFragment = new RecommendPageFragment();
                    mTransaction.add(R.id.fl_fragment_content,mRecommendPageFragment);    //通过事务将内容添加到内容页
                }else{
                    mTransaction.show(mRecommendPageFragment);
                }
                break;
            //个人中心
            case R.id.rl_four_layout:
                seleted();
                mRlFourLayout.setSelected(true);
                if (mMinePageFragment == null) {
                    mMinePageFragment = new MinePageFragment();
                    mTransaction.add(R.id.fl_fragment_content,mMinePageFragment);    //通过事务将内容添加到内容页
                }else{
                    mTransaction.show(mMinePageFragment);
                }
                break;
        }
        mTransaction.commit();
    }

    //设置所有按钮都是默认都不选中
    private void seleted() {
        mRlFirstLayout.setSelected(false);
        mRlSecondLayout.setSelected(false);
        mRlThirdLayout.setSelected(false);
        mRlFourLayout.setSelected(false);
    }

    //删除所有fragmtne
    private void hideAllFragment(FragmentTransaction transaction) {
        if (mHomePageFragment != null) {
            transaction.hide(mHomePageFragment);
        }
        if (mMatchPageFragment != null) {
            transaction.hide(mMatchPageFragment);
        }
        if (mRecommendPageFragment != null) {
            transaction.hide(mRecommendPageFragment);
        }
        if (mMinePageFragment != null) {
            transaction.hide(mMinePageFragment);
        }
    }
}

HomePageFragment 其中的一个界面 首页

package com.example.matsonga.tabbaractivity;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class HomePageFragment extends BasePageTitleFragent {

    @Override
    protected View initView() {
        setTitleIcon("首页",true); //设置第一个首页不显示标题
        View homeFragment = View.inflate(getContext(), R.layout.fg_homepage, null);
        return homeFragment ;
    }

    @Override
    protected void initData() {

    }
}

homepage的drawabe 其他界面和他一样 改一下图片名字就行

屏幕快照 2019-05-27 下午11.55.10.png
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/tab_assistant_gray" android:state_selected="true" />
    <item android:drawable="@drawable/tab_assistant_light" />
</selector>

homepage的XML 其他界面和他一样 内容自定义

屏幕快照 2019-05-27 下午11.56.28.png
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


</LinearLayout>

相关文章

网友评论

    本文标题:android创建类似iOS的tabbar如何实现

    本文链接:https://www.haomeiwen.com/subject/svsltctx.html