ساخت custom tab برای tablayout در اندروید

با سلام، تو این پست در مورد گذاشتن آیکون برای تب های tablayout صحبت کرده بودم. یکی از مشکلاتی که دراون روش مطرح هست اینه که ما نمیتونیم سایز آیکونی که استفاده کردیم را تغییر بدهیم. برای حل مشکل یکی روش های مختلفی وجود داره (تو نت یه جستجو کنید به چند تا روش دسترسی پیدا می کنید)، اما با توجه به جستجوی خودم بهترنی روش ساخت یه customlayout برای هر تب هست.

روش کار به این صورت که اول یک فایل layout با نام custom_main_tab می سازیم.

کد زیر را جایگزین کد پیشفرض layout می کنیم:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitCenter"
        android:id="@+id/icon"
        android:layout_gravity="center_horizontal"
        android:paddingBottom="10dp"
        android:layout_margin="5dp"/>

</LinearLayout>

حال نوبت به این میرسه که به هر تب از تب بار بگیم که باید از این layout جدید که در بالا ساختیم استفاده کنه و آیکون هر تب نیز متفاوت باشه به این منظرو داخل فایل جاوایی که talayout رو ساختیم (برای من داخل mainactivity) کد های زیر را وارد می کنیم (اپ تست من 4 تا تب داشت):

//////// sadegh set custom view for each tab in tablayout //// sadegh tablayout tab icon setting
        View view1 = getLayoutInflater().inflate(R.layout.custom_main_tab, null);
        view1.findViewById(R.id.icon).setBackgroundResource(R.drawable.menu_contacts_128);
        tabLayout.getTabAt(0).setCustomView(view1);

        View view2 = getLayoutInflater().inflate(R.layout.custom_main_tab, null);
        view2.findViewById(R.id.icon).setBackgroundResource(R.drawable.menu_my_event_128);
        tabLayout.getTabAt(1).setCustomView(view2);

        View view3 = getLayoutInflater().inflate(R.layout.custom_main_tab, null);
        view3.findViewById(R.id.icon).setBackgroundResource(R.drawable.menu_other_event_128);
        tabLayout.getTabAt(2).setCustomView(view3);

        View view4 = getLayoutInflater().inflate(R.layout.custom_main_tab, null);
        view4.findViewById(R.id.icon).setBackgroundResource(R.drawable.menu_request_128);
        tabLayout.getTabAt(3).setCustomView(view4);

تمام ….