第一阶段冲刺第一天

import android.os.Bundle;

import android.util.DisplayMetrics;
import android.view.View;
import android.view.LayoutInflater;

import android.widget.ImageView;

import android.widget.TextView;


import com.example.myapplication.adapter.MainFragmentAdapter;

/**
 * @author:created By ZhangHao
 * 时间:2019/4/23  20:45
 * 邮箱:188660586@qq.com
 * 当前文件:MainActivity
 */
//主框架

public class MainActivity extends AppCompatActivity {
    private final int[] TAB_TITLES = new int[]{
            R.string.menu_msg, R.string.menu_history, R.string.menu_mine
    };
    private final int[] TAB_IMGS = new int[]{
            R.drawable.tab_main_msg, R.drawable.tab_main_history, R.drawable.tab_main_mine
    };
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;

    private PagerAdapter pagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      /*  Window win=getWindow();
        win.requestFeature(Window.FEATURE_RIGHT_ICON);*/

        setContentView(R.layout.activity_main);

        ButterKnife.bind(this);
        initPager();
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        String strOpt = "手机屏幕分辨率为:" + dm.widthPixels + " × " + dm.heightPixels;
        System.out.println(strOpt);
        setTabs(tabLayout, getLayoutInflater(), TAB_TITLES, TAB_IMGS);
        /* win.setFeatureDrawableResource(Window.FEATURE_RIGHT_ICON, R.drawable.subway_title);*/
    }

    private void setTabs(TabLayout tabLayout, LayoutInflater inflater, int[] tabTitles, int[] tabImgs) {
        for (int i = 0; i < tabImgs.length; i++) {
            TabLayout.Tab tab = tabLayout.newTab();
            View view = inflater.inflate(R.layout.item_main_menu, null);
            //使用自定义视图,便于修改
            tab.setCustomView(view);
            TextView tvTitle = (TextView) view.findViewById(R.id.txt_tab);
            tvTitle.setText(tabTitles[i]);
            ImageView imgTab = (ImageView) view.findViewById(R.id.img_tab);
            imgTab.setImageResource(tabImgs[i]);
            tabLayout.addTab(tab);
        }
    }

    private void initPager() {
        pagerAdapter = new MainFragmentAdapter(getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition(), false);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }


}

 

上一篇:Android项目开发之--------地铁时光机(一,搭建主框架)


下一篇:关于android.support.design.widget.TabLayout无法使用