我试图增加我的应用程序中的标签图标大小.图标尺寸是固定的尝试了很多方式,但没有任何工作,最后尝试以下,但没有变化的大小.请问如果有人能告诉我正确的方式,我会很高兴.提前谢谢.
这是我的代码,
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
<style name="AppTheme.ActionBar" parent="Widget.AppCompat.Light.ActionBar">
<item name="android:layout_width">50dp</item>
<item name="android:layout_height">50dp</item>
</style>
tablayout.xml
<android.support.design.widget.TabLayout
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"/>
解决方法
嗨,我面临同样的问题,这是我能找到的最好的解决方案:
您应该使用(setCustomView),首先创建一个新的布局文件
让它命名(customtab.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:id="@+id/icon"
android:layout_gravity="center_horizontal" />
</LinearLayout>
然后为每个选项卡执行此操作:(使用相同的布局.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)); ...