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

时间:2019-04-16 12:23:57

标签: java android android-tablayout android-icons

我的程序在[callController requestTransaction:transaction completion:^(NSError* _Nullable error) { if (error) NSLog(@"iOS Call Start Error: %@", error); else { CXCallUpdate* update = [CXCallUpdate new]; update.supportsHolding = NO; [provider reportCallWithUUID:uuid updated:update]; } }]; 上包含图标。我只看到了一种从stackOverflow调整tabLayout上图标大小的方法。我试图按照我看到的方式做。但是只有我可以调整tabItem上第一个图标的大小。

MainActivity.java

tabLayout

customtab.xml

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);

    }

Only car icon is big. All icon beside car must be big. Logcat

1 个答案:

答案 0 :(得分:3)

尝试这样做

在活动中的某处准备标签页

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]; 
          }
      }        
  }

您的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="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:id="@+id/icon"
        android:layout_gravity="center_horizontal" />

</LinearLayout>