美文网首页Android TipsAndroid UIAndroid 从建项目开始-(完整项目)
Android从0到完整项目(2)启动页与底部导航

Android从0到完整项目(2)启动页与底部导航

作者: 移动开发者_李挺哲 | 来源:发表于2017-04-21 13:11 被阅读246次

    实现启动页与底部导航

    前言

    刚工作的时候与写andBase大神一起工作,所以用了很多他造的轮子,现在也会用到一些常用的工具类,封装成自己的库。

    1. 使用Android 导入库 myLibary

    http://download.csdn.net/detail/chinaltz/9811788

    点击File--->New---->New Module 项目 点右键 ---> OPen Module Settings 点+ 号 完成 启动页面 主页

    2.实现

    一.启动页面 实现可以有多种
    (1)倒计时

     public class MyCount extends CountDownTimer {
    
    
            public MyCount(long millisInFuture, long countDownInterval) {
                super(millisInFuture, countDownInterval);
            }
    
            @Override
            public void onFinish() {
    
                Intent in = new Intent(mContext, MainActivity.class);
                startActivity(in);
                finish();
    
            }
    
            @Override
            public void onTick(long millisUntilFinished) {
    
                long second = millisUntilFinished / 1000;
                countButton.setText("跳过|" + second + "s");
    
            }
    
        }
    

    (2)延时

       new Handler().postDelayed(new Runnable() {
                public void run() {
                    /* Create an Intent that will start the Main WordPress Activity. */
                    Intent mainIntent = new Intent(splashScreen.this, wpAndroid.class);
                    splashScreen.this.startActivity(mainIntent);
                    splashScreen.this.finish();
                }
            }, 2900);
    

    二.底部导航(Tab)

    1. ViewPager + RadioButton
      http://blog.csdn.net/chinaltz/article/details/40817753
      2.Fragment+RadioButton
      http://blog.csdn.net/chinaltz/article/details/40145407
    2. ViewPager +TabLayout
      本例实现
    tabLayout.setupWithViewPager(viewPager);
    
            //设置ViewPager 是否可以滑动
            viewPager.setPagingEnabled(true);
    
            viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    
                }
    
                @Override
                public void onPageSelected(int position) {
    
    
                    for (int i = 0; i < titleList.length; i++) {
                        TabLayout.Tab tab = tabLayout.getTabAt(i);
                        View view = tab.getCustomView();
                        ImageView img = (ImageView) view.findViewById(R.id.tab_icon);
                        TextView title = (TextView) view.findViewById(R.id.tab_title);
    
                        if (position == i) {
                            img.setImageResource(icons_press[i]);
                            title.setTextColor(getResources().getColor(R.color.mainColor));
                        } else {
                            img.setImageResource(icons[i]);
                            title.setTextColor(getResources().getColor(R.color.gray));
                        }
    
    
                    }
                }
    
                @Override
                public void onPageScrollStateChanged(int state) {
    
                }
            });
    
    
            //为TabLayout添加tab名称
            for (int i = 0; i < titleList.length; i++) {
                TabLayout.Tab tab = tabLayout.getTabAt(i);
                tab.setCustomView(getTabView(i));
            }
    
    

    /**
    * 添加getTabView的方法,来进行自定义Tab的布局View
    *
    * @param position
    * @return
    */
    public View getTabView(int position) {
    LayoutInflater mInflater = LayoutInflater.from(this);
    View view = null;

        view = mInflater.inflate(R.layout.item_bottom_tab, null);
        AutoUtils.auto(view);
    
        TextView tv = (TextView) view.findViewById(R.id.tab_title);
        tv.setText(titleList[position]);
        ImageView img = (ImageView) view.findViewById(R.id.tab_icon);
        if (position == 0) {
            tv.setTextColor(getResources().getColor(R.color.mainColor));
            img.setImageResource(icons_press[position]);
        } else {
            tv.setTextColor(getResources().getColor(R.color.gray));
            img.setImageResource(icons[position]);
        }
        return view;
    }
    
    
    4.BottomNavigationView 新版Android 创建项目 可以学习
    
    ![BottomNavigationView](http:https://img.haomeiwen.com/i2595860/16b8721a75d3696c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    
    
    
    GitHub   代码在这  https://github.com/chinaltz/JustAndroid
    会不定时更新

    相关文章

      网友评论

        本文标题:Android从0到完整项目(2)启动页与底部导航

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