如何在TabLayout中设置图标大小?

时间:2016-01-08 04:21:40

标签: android android-tablayout

这是我的代码:

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

图标的大小取决于图像大小。我怎么能调整它?

6 个答案:

答案 0 :(得分:6)

设置图标填充

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

答案 1 :(得分:0)

更改图标大小 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 

            }

答案 2 :(得分:0)

要获得对大小的完全控制,我建议使用自定义视图来呈现标签。

首先为此示例创建一个名为“my_custom_tab”的新布局

<?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"/>

现在,在您的代码中设置自定义图片

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

答案 3 :(得分:0)

你可以这样做:

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

答案 4 :(得分:0)

如果您不想将xml用于自定义视图。只需在运行时创建imageview并将其添加到特定选项卡的tablayout中即可。

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

它看起来像这样。

enter image description here

答案 5 :(得分:0)

我决定结束这个问题。由于我们实际上无法在TabLayout中设置图标的特定大小,而只能对其进行填充。 参考: https://developer.android.com/guide/practices/ui_guidelines/icon_design_tab