如何调整 tabItem 上的图标大小?

How can I resize icon on tabItem?

我的程序包含 tabLayout 上的图标。我只看到一种方法可以从 Whosebug 调整 tabItem 上的图标大小。我试着按照我看到的方式去做。但只有我可以调整 tabLayout.

上第一个图标的大小

MainActivity.java

LinearLayout lilcon;


private Integer[] images;
Integer[] domesticImages = {
        R.drawable.ic_directions_bus_black_24dp,
        R.drawable.ic_flight_takeoff_black_24dp,
        R.drawable.ic_directions_boat_black_24dp,
        R.drawable.ic_train_black_24dp,
        R.drawable.ic_hotel,
        R.drawable.ic_rent_a_car
};
private Integer[] domesticImagesClicked = {
        R.drawable.ic_directions_bus_clicked_24dp,
        R.drawable.ic_flight_takeoff_clicked_24dp,
        R.drawable.ic_directions_boat_clicked_24dp,
        R.drawable.ic_train_clicked_24dp,
        R.drawable.ic_hotel_clicked,
        R.drawable.ic_rent_a_car_clicked
};
private Integer[] abroadImages = {
        R.drawable.ic_flight_takeoff_black_24dp,
        R.drawable.ic_hotel,
        R.drawable.ic_directions_bus_black_24dp,
        R.drawable.ic_directions_boat_black_24dp,
        R.drawable.ic_train_black_24dp,
        R.drawable.ic_rent_a_car
};
private Integer[] abroadImagesClicked = {
        R.drawable.ic_flight_takeoff_clicked_24dp,
        R.drawable.ic_hotel_clicked,
        R.drawable.ic_directions_bus_clicked_24dp,
        R.drawable.ic_directions_boat_clicked_24dp,
        R.drawable.ic_train_clicked_24dp,
        R.drawable.ic_rent_a_car_clicked
};

Integer[] domesticImagesTablet = {
        R.drawable.ic_tablet_destination_bus,
        R.drawable.ic_tablet_airplane,
        R.drawable.ic_tablet_boat,
        R.drawable.ic_tablet_train,
        R.drawable.ic_tablet_hotel,
        R.drawable.ic_tablet_rent_a_car
};
private Integer[] domesticImagesClickedTablet = {
        R.drawable.ic_tablet_destination_bus_clicked,
        R.drawable.ic_tablet_airplane_clicked,
        R.drawable.ic_tablet_boat_clicked,
        R.drawable.ic_tablet_train_clicked,
        R.drawable.ic_tablet_hotel_clicked,
        R.drawable.ic_tablet_rent_a_car_clicked
};
private Integer[] abroadImagesTablet = {
        R.drawable.ic_tablet_airplane,
        R.drawable.ic_tablet_hotel,
        R.drawable.ic_tablet_destination_bus,
        R.drawable.ic_tablet_boat,
        R.drawable.ic_tablet_train,
        R.drawable.ic_tablet_rent_a_car
};
private Integer[] abroadImagesClickedTablet = {
        R.drawable.ic_tablet_airplane_clicked,
        R.drawable.ic_tablet_hotel_clicked,
        R.drawable.ic_tablet_destination_bus_clicked,
        R.drawable.ic_tablet_boat_clicked,
        R.drawable.ic_tablet_train_clicked,
        R.drawable.ic_tablet_rent_a_car_clicked
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_result);
    Log.i("stack", "ResultCreated");
    init();
    initData();
    attachFragments();
    /* registerCartOperationsReceiver();*/
    registerHandlers();
}
void init() {
    lilcon = view.findViewById(R.id.licon);
}
void initData(){
    if (isDomestic) {
        images = domesticImages;
    } else {
        images = abroadImages;
    }
}

@Override
public void onTabSelected(TabLayout.Tab tab) {
    boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
  /*  if (tabletSize) {
        if (isDomestic) {
            lilcon.setBackgroundResource(R.drawable.ic_tablet_destination_bus_clicked);
            //   tab.setIcon(domesticImagesTablet[i]);
            tabLayout.getTabAt(0).setCustomView(lilcon);
        }else{
            lilcon.setBackgroundResource(R.drawable.ic_tablet_airplane_clicked);
            //   tab.setIcon(domesticImagesTablet[i]);
            tabLayout.getTabAt(0).setCustomView(lilcon);
        }
    }else{}*/
    if (tab != null && tab.getIcon() != null) {
        int i = tab.getPosition();
        if (isDomestic) {
            tab.setIcon(domesticImagesClicked[i]);
            // lilcon.setBackgroundResource(domesticImages[i]);
           // tab.setCustomView(lilcon);
        } else{
            tab.setIcon(abroadImagesClicked[i]);
           // lilcon.setBackgroundResource(domesticImages[i]);
           // tab.setCustomView(lilcon);
        }
     }


/*    int pos = tab.getPosition();
    for (int i = 0; i < tabLayout.getTabCount(); i++) {
        View aview = tabLayout.getTabAt(i).getCustomView();
        ImageView img = aview.findItemById(R.id.icon);
        if (i == pos) {
            if (isDomestic)
                img.setImageResource(domesticImagesClicked[i];
            else
                img.setImageResource(abroadImagesClicked[i];
        } else {
            if (isDomestic)
                img.setImageResource(domesticImages[i];
            else
                img.setImageResource(abroadImages[i];
        }
    }
@Override
public void onTabUnselected(TabLayout.Tab tab) {
    if (tab != null && tab.getIcon() != null) {
        int i = tab.getPosition();
       /* boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
        if (tabletSize) {
            if (isDomestic) {
                lilcon.setBackgroundResource(R.drawable.ic_tablet_destination_bus_clicked);
            //   tab.setIcon(domesticImagesTablet[i]);
                 tabLayout.getTabAt(0).setCustomView(lilcon);
                //  lilcon.setBackgroundResource(R.drawable.ic_tablet_destination_bus_clicked);
               // tab.getTabAt(0).setCustomView(lilcon);
                //  lilcon.setBackgroundResource(domesticImages[i]);
                //  tab.setCustomView(lilcon);
            }else{
               // tab.setIcon(abroadImagesTablet[i]);
                lilcon.setBackgroundResource(R.drawable.ic_tablet_airplane_clicked);
                //   tab.setIcon(domesticImagesTablet[i]);
                tabLayout.getTabAt(0).setCustomView(lilcon);
                //tab.setIcon(abroadImages[i]);
                //  lilcon.setBackgroundResource(domesticImages[i]);
                //  tab.setCustomView(lilcon);
            }
        }
        else{}*/
        if (isDomestic) {
            tab.setIcon(domesticImages[i]);
          //  lilcon.setBackgroundResource(domesticImages[i]);
          //  tab.setCustomView(lilcon);
        }else{
            tab.setIcon(abroadImages[i]);
          //  lilcon.setBackgroundResource(domesticImages[i]);
          //  tab.setCustomView(lilcon);
        }
    }
}
@Override
public void onTabReselected(TabLayout.Tab tab) {    }
void attachFragments() {
    try {
        for (int i = 0; i < tabLayout.getTabCount(); i++) {          
        }      
        boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
        if (tabletSize) {
            if (isDomestic){
            //     tabLayout.getTabAt(0).setIcon(R.drawable.ic_icon_bus);
                lilcon.setBackgroundResource(R.drawable.ic_tablet_destination_bus_clicked);
                tabLayout.getTabAt(0).setCustomView(lilcon);
            } else {
               // tabLayout.getTabAt(0).setIcon(R.drawable.ic_flight_takeoff_clicked_24dp);
                 lilcon.setBackgroundResource(R.drawable.ic_tablet_airplane_clicked);
                 tabLayout.getTabAt(0).setCustomView(lilcon);
        }
        }else {
        if (isDomestic){
            tabLayout.getTabAt(0).setIcon(R.drawable.ic_directions_bus_clicked_24dp);
          //  lilcon.setBackgroundResource(R.drawable.ic_directions_bus_clicked_24dp);
        //   tabLayout.getTabAt(0).setCustomView(lilcon);
        } else {
            tabLayout.getTabAt(0).setIcon(R.drawable.ic_flight_takeoff_clicked_24dp);
           // lilcon.setBackgroundResource(R.drawable.ic_flight_takeoff_clicked_24dp);
           // tabLayout.getTabAt(0).setCustomView(lilcon);
        }}

        // tabLayout.getTabAt(0).setCustomView(vehicleTypeImageView(1,true));
        if (tabletSize){
        }else{}
        for (int i = 1; i < tabLayout.getTabCount(); i++) {
            if (isDomestic) {
                tabLayout.getTabAt(i).setIcon(domesticImages[i]);

             //    lilcon.setBackground(domesticImages[i]);
             //    tabLayout.getTabAt(i).setCustomView(lilcon);

            } else {
                 tabLayout.getTabAt(i).setIcon(abroadImages[i]);
              //  lilcon.setBackgroundResource(abroadImages[i]);
              //  tabLayout.getTabAt(i).setCustomView(lilcon);
            }
        }
      /*  for (int i = 1; i < tabLayout.getTabCount(); i++) {
            if (isDomestic)
                lilcon.setImageResource(domesticImages[i]);
            else
                lilcon.setImageResource(abroadImages[i]);
            tabLayout.getTabAt(i).setCustomView(lilcon);
        }*/
        // herhangi bir tab item seçildiğinde tetiklenecek yer
        tabLayout.addOnTabSelectedListener(this);

    }

customtab.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/licon"
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" />
  </LinearLayout>

试着这样做

在 activity

中的某处准备标签
for (int i = 0; i < tabLayout.getTabCount(); i++) {
    View view1 = getLayoutInflater().inflate(R.layout.customtab, null);
    ImageView img = view1.findViewById(R.id.icon);
    img.setScaleType(ScaleType.FIT_XY);
    if (domestic)
        img.setImageResource(domesticImages[i]);
    else
        img.setImageResource(abroadImages[i]);
    tabLayout.getTabAt(i).setCustomView(view1);
}

然后,

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

    int pos = tab.getPosition();
    for (int i = 0; i < tabLayout.getTabCount(); i++) {
        View aview = tabLayout.getTabAt(i).getCustomView();
        ImageView img = aview.findViewById(R.id.icon);
        if (i == pos) {
            if (isDomestic) 
                img.setImageResource(domesticImagesClicked[i];
            else
                img.setImageResource(abroadImagesClicked[i]; 
         } else {
            if (isDomestic) 
                img.setImageResource(domesticImages[i];
            else
                img.setImageResource(abroadImages[i]; 
          }
      }        
  }

您的自定义选项卡xml

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

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:id="@+id/icon"
        android:layout_gravity="center_horizontal" />

</LinearLayout>