tablayout 增加数字小标,如何在TabLayout中增加选项卡的图标大小

I am trying to increase icon size of tabs in my app. Icon sizes are fixed tried out many ways but nothing is working, finally tried the following but no change in size.Please if any one can tell me the right way I will be glad.Thanks in advance.

Here is my code,

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

setSupportActionBar(toolbar);

TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);

tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.my1));

tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.feed_s));

tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.add_ds1));

tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.history_ds));

tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

style.xml

50dp

50dp

tablayout.xml

android:id="@+id/tab_layout"

android:layout_width="match_parent"

android:layout_height="60dp"

android:layout_below="@+id/toolbar"

android:background="?attr/colorPrimary"

android:elevation="6dp"

android:theme="@style/AppTheme.ActionBar"/>

解决方案

Hi I faced the same problem before and this is the best solution I could find:

You should use (setCustomView), first of all make a new layout file

lets name it (customtab.xml):

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:scaleType="fitCenter"

android:id="@+id/icon"

android:layout_gravity="center_horizontal" />

Then and for each tab do this: (use the same layout .xml)

View view1 = getLayoutInflater().inflate(R.layout.customtab, null);

view1.findViewById(R.id.icon).setBackgroundResource(R.drawable.my1);

tabLayout.addTab(tabLayout.newTab().setCustomView(view1));

View view2 = getLayoutInflater().inflate(R.layout.customtab, null);

view2.findViewById(R.id.icon).setBackgroundResource(R.drawable.my2);

tabLayout.addTab(tabLayout.newTab().setCustomView(view2));

View view3 = getLayoutInflater().inflate(R.layout.customtab, null);

view3.findViewById(R.id.icon).setBackgroundResource(R.drawable.my3);

tabLayout.addTab(tabLayout.newTab().setCustomView(view3));

...

OR

in a similar way

public static final int[] tabIcon = {R.drawable.icon_one, R.drawable.icon_two, R.drawable.icon_three};

private void setCustomTabs() {

for (int i = 0; i < tabIcon.length; i++) {

View view = getLayoutInflater().inflate(R.layout.customtab,null);

TabLayout.Tab tab = tabLayout.getTabAt(i);

view.findViewById(R.id.icon).setBackgroundResource(tabIcon[i]);

if(tab!=null) tab.setCustomView(view);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值