Design包下TabLayout的簡單使用

Android design支持庫中的TabLayout一般都用來實(shí)現(xiàn)頭部Tab的效果。例如:


m3.png

但是像微信這種底部Tab布局在我們實(shí)際項(xiàng)目中還是非常常見的設(shè)計(jì),現(xiàn)在我們也可以用TabLayout非常方便的實(shí)現(xiàn)。

TabLayout.gif

布局

下面開始實(shí)現(xiàn)底部Tab,layout布局比較簡單,只用把TabLayout放置在底部即可。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <android.support.v4.view.ViewPager
        android:id="@+id/Tab_ViewPager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    </android.support.v4.view.ViewPager>
    <android.support.design.widget.TabLayout
        android:id="@+id/TabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/MyCustomTabLayout">
    </android.support.design.widget.TabLayout>
</LinearLayout>

自定義的style,把tabIndicatorHeight設(shè)為0dp。

    <style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
        <item name="tabIndicatorColor">?attr/colorAccent</item>
        <item name="tabIndicatorHeight">0dp</item>
        <item name="tabPaddingStart">12dp</item>
        <item name="tabPaddingEnd">12dp</item>
        <item name="tabBackground">@color/colorgrey</item>
        <item name="tabSelectedTextColor">?android:textColorPrimary</item>
    </style>

代碼實(shí)現(xiàn)

首先設(shè)置好ViewPager,然后設(shè)置TabLayout與ViewPager的對應(yīng)關(guān)系,最后最關(guān)鍵的是使用TabLayout的setCustomView設(shè)置自定義的TAB View。

public class TabLayoutActivity extends AppCompatActivity {
    private static final String TAG = "TabLayoutActivity";
    private  ViewPager mViewPager;
    private TabLayout mTabLayout;
    private MyAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_layout);
        mViewPager = (ViewPager) findViewById(R.id.Tab_ViewPager);
        mTabLayout = (TabLayout) findViewById(R.id.TabLayout);
        mAdapter = new MyAdapter(getSupportFragmentManager(),this);
        mViewPager.setAdapter(mAdapter);
        mTabLayout.setupWithViewPager(mViewPager);


//        mViewPager.setCurrentItem(0);

        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                changeTabSelect(tab);
            }

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

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

            }
        });

        for (int i = 0;i<mTabLayout.getTabCount();i++){
            TabLayout.Tab tab = mTabLayout.getTabAt(i);
            Log.d(TAG, "onCreate: getChildCount"+mTabLayout.getChildCount());
            if (tab!=null){
                Log.d(TAG, "onCreate: tab!=null  "+i);
                tab.setCustomView(mAdapter.getTabView(i));
            }
        }
    }
    private void changeTabSelect(TabLayout.Tab tab) {
        View view = tab.getCustomView();
        ImageView img_title = (ImageView) view.findViewById(R.id.imageview);
        TextView txt_title = (TextView) view.findViewById(R.id.textview);
        txt_title.setTextColor(Color.GREEN);
        if (txt_title.getText().toString().equals("標(biāo)題一")) {
            img_title.setImageResource(R.mipmap.homepage_select);
            mViewPager.setCurrentItem(0);
        } else if (txt_title.getText().toString().equals("標(biāo)題二")) {
            img_title.setImageResource(R.mipmap.startorder_select);
            mViewPager.setCurrentItem(1);
        } else {
            img_title.setImageResource(R.mipmap.shopcar_select);
            mViewPager.setCurrentItem(2);
        }
    }

    private void changeTabNormal(TabLayout.Tab tab) {
        View view = tab.getCustomView();
        ImageView img_title = (ImageView) view.findViewById(R.id.imageview);
        TextView txt_title = (TextView) view.findViewById(R.id.textview);
        txt_title.setTextColor(Color.BLACK);
        if (txt_title.getText().toString().equals("標(biāo)題一")) {
            img_title.setImageResource(R.mipmap.homepage_normal);
        } else if (txt_title.getText().toString().equals("標(biāo)題二")) {
            img_title.setImageResource(R.mipmap.startorder_normal);
        } else {
            img_title.setImageResource(R.mipmap.shopcar_normal);
        }
    }
}

其中changeTabSelect(TabLayout.Tab tab)與changeTabNormal(TabLayout.Tab tab)是用來實(shí)現(xiàn)Tab頁轉(zhuǎn)換的時候圖片以及文字顏色的改變。
Fragment中:

public class PageFragment extends Fragment{
    public static final String ARG_PAGE = "ARG_PAGE";
    private int mPage;

    public static PageFragment newInstance(int page) {
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, page);
        PageFragment pageFragment = new PageFragment();
        pageFragment.setArguments(args);
        return pageFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPage = getArguments().getInt(ARG_PAGE);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_page, container, false);
//        TextView textView = (TextView) view;
        TextView textView = (TextView) view.findViewById(R.id.Fragment_Page_TV);
        textView.setText("Fragment #" + mPage);
        return view;
    }
}

在MyAdapter中:

public class MyAdapter extends FragmentPagerAdapter{
    private final int PAGE_COUNT = 3;
    private String[] tabs = new String[]{"標(biāo)題一","標(biāo)題二","標(biāo)題三"};
    private Context context;
    private int imageResId[] = new int[]{R.mipmap.homepage_normal,R.mipmap.startorder_normal,R.mipmap.shopcar_normal};
    private int imageResPress[] = new int[]{R.mipmap.homepage_select,R.mipmap.startorder_select,R.mipmap.shopcar_select};
    public MyAdapter(FragmentManager fm,Context context) {
        super(fm);
        this.context = context;
    }

    @Override
    public Fragment getItem(int position) {
        return PageFragment.newInstance(position+1);
    }

    @Override
    public int getCount() {
        return PAGE_COUNT;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return tabs[position];
    }
    public View getTabView(int position) {
        View v = LayoutInflater.from(context).inflate(R.layout.custom_tab, null);
        TextView tv = (TextView) v.findViewById(R.id.textview);
        tv.setText(tabs[position]);
        ImageView img = (ImageView) v.findViewById(R.id.imageview);
        img.setImageResource(imageResId[position]);
        if (position == 0) {
            tv.setTextColor(Color.GREEN);
            img.setImageResource(imageResPress[position]);
        } else {
            tv.setTextColor(Color.BLACK);
            img.setImageResource(imageResId[position]);
        }
        return v;
    }
}

其中g(shù)etTabView(int position)是用來初始化Tab頁面的圖標(biāo)與文字的。

 public View getTabView(int position) {
        View v = LayoutInflater.from(context).inflate(R.layout.custom_tab, null);
        TextView tv = (TextView) v.findViewById(R.id.textview);
        tv.setText(tabs[position]);
        ImageView img = (ImageView) v.findViewById(R.id.imageview);
        img.setImageResource(imageResId[position]);
        if (position == 0) {
            tv.setTextColor(Color.GREEN);
            img.setImageResource(imageResPress[position]);
        } else {
            tv.setTextColor(Color.BLACK);
            img.setImageResource(imageResId[position]);
        }
        return v;
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,979評論 25 709
  • 內(nèi)容抽屜菜單ListViewWebViewSwitchButton按鈕點(diǎn)贊按鈕進(jìn)度條TabLayout圖標(biāo)下拉刷新...
    皇小弟閱讀 47,147評論 22 665
  • 人,是一種群居動物,大都在與形形色色的人相處。也有些時候,我們獨(dú)自一人,恍若置身荒島,孤立無援,那種心靈深處散發(fā)孤...
    小米蟲子閱讀 520評論 0 0
  • 上一篇文章發(fā)在朋友圈以后,收到了男女主角的巨額打賞。張姑娘看見了…… 她:寫我寫我,我也打賞。 我:吼,你的趣事太...
    六洱閱讀 746評論 14 11
  • 一場冷雨澆透了燥熱 一口深吸嘗到了秋之味 慢慢升騰薄霧氤氳了淡淡憂傷 仿佛又回到了不曾離開的來時路 那時候 我們總...
    一葉漁舟閱讀 419評論 2 1

友情鏈接更多精彩內(nèi)容