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

9

这是我的代码:

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

图标的大小是根据图像大小而定的。我该如何调整它的大小?

从XML中更改高度和宽度。 - Aditya Vyas-Lakhan
尝试使用编辑器重新调整图标大小。 - shine_joseph
设置TabLayout的高度以增加图标的大小。 - Anand Savjani
请查看此链接:https://developer.android.com/guide/practices/ui_guidelines/icon_design_tab - Sreejesh K Nair
7个回答

6

设置图标的内边距

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

7
地球上到底是什么东西是tablayout.getTabWidget()?它似乎不存在? - Neon Warge

2

如果您不想使用XML创建自定义视图,可以在运行时创建ImageView并将其添加到特定选项卡的TabLayout中。

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

它会看起来像这样。

图片描述在此处输入


0

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
我在这行代码 view_bottom_tabLayout.getChildAt(i).setLayoutParams(params); 报了一个 NPE 错误。 - Ken Tan
你确定在xml中使用了这个android.support.design.widget.TabLayout吗?并且确保将子元素添加到视图中,并在之后使用以下代码行。 - SaravanaRaja

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

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

0

有一段代码对我有效,就是上面Deepak Sachdeva的答案,我只是稍微编辑了一下,现在可以百分之百地工作了。一开始我添加了一个带有自定义图像的选项卡。

    ImageView imgView= new ImageView(getApplicationContext());
      imgView.setImageResource(R.drawable.icon);
      imgView.setScaleType(ImageView.ScaleType.FIT_CENTER);
    
    imgView.setLayoutParams(new LinearLayout.LayoutParams(100, 100));

tablayout1.addTab(tablayout1.newTab().setCustomView(imgView));

然后对所有选项卡都执行此操作,通过这种方式添加它们。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接