1. 程式人生 > >Android筆記——自定義TabLayout之title與icon

Android筆記——自定義TabLayout之title與icon

自定義TabLayout

這裡寫圖片描述

總佈局

實現的是底端TabLayout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support
.v4.view.ViewPager android:id="@+id/vp_content_home" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/tl_tab_home"/> <android.support.design.widget.TabLayout android:id="@+id/tl_tab_home" android:layout_width="match_parent"
android:layout_height="wrap_content" android:background="#fff" app:tabIndicatorHeight="0dp" app:tabMode="scrollable" app:tabSelectedTextColor="@color/darkgreen" android:layout_alignParentBottom="true" android:elevation="@dimen/elevation_5"/> </RelativeLayout>

TabLayout自定義佈局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <ImageView
        android:id="@+id/tab_icon"
        android:layout_width="@dimen/back_size"
        android:layout_height="@dimen/back_size"/>

    <TextView
        android:id="@+id/tab_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@drawable/selector_text_tab"
        android:textSize="@dimen/text_size_15"
        android:enabled="false"/>

</LinearLayout>

Selector

color

<item  android:state_selected="true" android:color="@color/darkgreen"></item>
<item  android:color="@color/darkgray"></item>
<item  android:state_pressed="true" android:color="@color/darkgreen"/>
<item  android:state_enabled="true" android:color="@color/darkgreen"/>

icon

因為三個圖示的selector是一樣的,所以就取了一個

<item android:state_selected="true" android:drawable="@drawable/icon_dynamics_select"></item>
<item android:drawable="@drawable/icon_dynamics_unselect"></item>
<item android:state_pressed="true" android:drawable="@drawable/icon_dynamics_select"/>

程式碼

主要分以下兩步實現,若想看怎麼自定義,請直接跳到最後的getTabView

initContent();
initTab();
String[] title = {"個人","推薦","動態"};

ContentPagerAdapter

class ContentPagerAdapter extends FragmentPagerAdapter {

        public ContentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return tabFragments.get(position);
        }

        @Override
        public int getCount() {
            return tabIndicators.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabIndicators.get(position);
        }
}

initContent

private void initContent(){
        tabIndicators = new ArrayList<>();
        for(int i = 0; i < title.length; i++){
            tabIndicators.add(title[i]);
        }
        tabFragments = new ArrayList<>();
        tabFragments.add(new UserFragment());
        tabFragments.add(new RecommendFragment());
        tabFragments.add(new DynamicsFragment());

        contentAdapter = new ContentPagerAdapter(getSupportFragmentManager());
        vp_home.setAdapter(contentAdapter);
}

initTab

private void initTab(){
        tab_home.setTabMode(TabLayout.MODE_FIXED);
        tab_home.setSelectedTabIndicatorHeight(0);
        ViewCompat.setElevation(tab_home,getResources().getDimension(R.dimen.elevation_5));
        tab_home.setupWithViewPager(vp_home);
        for(int i = 0; i < title.length;i++){
            TabLayout.Tab tab = tab_home.getTabAt(i);
            if(tab != null){
                tab.setCustomView(getTabView(i));
            }
        }
        tab_home.getTabAt(1).select();
}

getTabView

private View getTabView(int position){
        TypedArray prodIcon_tab = getResources().obtainTypedArray
                (R.array.prodIcon_tab);//圖片資源ID陣列
        View view = LayoutInflater.from(this).inflate(R.layout.tab_item,null);
        TextView tab_text = (TextView)view.findViewById(R.id.tab_text);
        tab_text.setText(title[position]);
        ImageView tab_icon = (ImageView)view.findViewById(R.id.tab_icon);
        tab_icon.setImageResource(prodIcon_tab.getResourceId(position,0));
        return view;
}