简体   繁体   中英

How to set icon size in TabLayout?

Here is my code:

private TabLayout tabLayout;
private int[] tabIcons = {
        R.mipmap.ic_compass,
        R.mipmap.ic_place,
        R.mipmap.ic_passport,
        R.mipmap.ic_setting
};


...
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
tabLayout.getTabAt(3).setIcon(tabIcons[3]);

The size of icon is according to image size. How could I resize it?

set icons padding

    for (int i = 0; i < tablayout.getTabWidget().getChildCount(); i++)
    {
        tablayout.getTabWidget().getChildAt(i).setPadding(10,10,10,10);
    }

change of Icon size android.support.design.widget.TabLayout

for (int i = 0; i < view_bottom_tabLayout.getTabCount(); i++)
            {
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(60, 60); //set new width & Height
                params.gravity = Gravity.CENTER; //set gravity back to center
                view_bottom_tabLayout.getChildAt(i).setLayoutParams(params);//set ur new params 

            }

To get total control of size I recommend using a custom view to render the tab.

First create a new layout - named "my_custom_tab" for this example

<?xml version="1.0" encoding="utf-8"?>

<!--Change the width, height, scaleType to whatever you like-->
<ImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="fitCenter"
    android:id="@+id/icon"
    android:layout_gravity="center_horizontal"
    android:src="@mipmap/ic_launcher"/>

Now in your code set the custom image

private void setUpTabs (ViewPager viewPager) {
  TabLayout tabs = (TabLayout) findViewById(R.id.tabs);
        if (tabs != null) {
            tabs.setupWithViewPager(viewPager);

   int tabCount = tabAdapter.getCount(); //Assuming you have already somewhere set the adapter for the ViewPager

            for (int i = 0; i < tabCount; i++) {
                TabLayout.Tab tab = tabs.getTabAt(i);
                if (tab != null){
                    ImageView myCustomIcon = (ImageView) LayoutInflater.from(tabs.getContext()).inflate(R.layout.my_custom_tab, null);

                    /*Here is where to set image if doing it dynamically
                    myCustomIcon.setImageBitmap(bitmap);
                    */

                    tab.setCustomView(myCustomIcon);
                }
            }
        }
}

you can do this:

LinearLayout ll = (LinearLayout) tabLayout.getChildAt(0);
    for (int i = 0; i < ll.getChildCount(); i++) {
        LinearLayout tabView = (LinearLayout) ll.getChildAt(i);
        for (int j = 0; j < tabView.getChildCount(); j++) {
            if (tabView.getChildAt(j) instanceof TextView) {
                TextView textView = (TextView) tabView.getChildAt(j);
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) textView.getLayoutParams();
                layoutParams.topMargin = 0;
                textView.setLayoutParams(layoutParams);
            } else if (tabView.getChildAt(j) instanceof ImageView) {
                ImageView imageView = (ImageView) tabView.getChildAt(j);
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) imageView.getLayoutParams();
                layoutParams.bottomMargin = 0;
                imageView.setLayoutParams(layoutParams);
            }
        }
    }

If you don't want to use xml for the custom view. Simply create imageview on runtime and add that in tablayout in a particular tab.

  ImageView imgView= new ImageView(MainActivity.this);
  imgView.setImageResource(drawableImage);
  imgView.setPadding(10,10,10,10)
  tabLayout.getTabAt(1).setCustomView(imgView);

It will look like this.

在此输入图像描述

I decide to close this question. Since we actually cannot set specific size for icon in TabLayout but padding to it. Ref: https://developer.android.com/guide/practices/ui_guidelines/icon_design_tab

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM