我正在尝试增加应用程序中选项卡的图标大小。图标大小是固定的尝试了许多方法,但没有任何工作,最后尝试了以下,但没有改变的size.Please,如果有人可以告诉我正确的方式,我将提前glad.Thanks。
这是我的代码,
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"/>
发布于 2016-02-13 05:07:08
嗨,我之前遇到过同样的问题,这是我能找到的最好的解决方案:
你应该使用(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));
...
或者以类似的方式
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);
}
}
发布于 2016-02-20 03:46:29
正如文档中所述,您可以使用带有android:id="@android:id/icon"
的ImageView
创建新布局,并将其设置为选项卡的自定义视图。TabLayout将使用android:id/icon
自动将图标放置在内部ImageView中
然后,在您的代码中,您可以在创建选项卡时调用setIcon(R.drawable.yourIcon)
。
然后,您可以在for循环中应用您的自定义布局:
mTabLayout.addTab(mTabLayout.newTab().setIcon(R.drawable.searchpin));
mTabLayout.addTab(mTabLayout.newTab().setIcon(R.drawable.discussionpin));
mTabLayout.addTab(mTabLayout.newTab().setIcon(R.drawable.userpin));
for (int i = 0; i < mTabLayout.getTabCount(); i++) {
TabLayout.Tab tab = mTabLayout.getTabAt(i);
if (tab != null) tab.setCustomView(R.layout.view_home_tab);
}
下面是xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@android:id/icon"
android:layout_width="@dimen/tab_icon_size"
android:layout_height="@dimen/tab_icon_size"
android:layout_centerInParent="true"/>
</RelativeLayout>
发布于 2016-10-19 17:35:31
不需要使用任何xml视图。创建imageview运行时并添加image as view :)
for (int i = 0; i < tabLayout.getTabCount(); i++) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(drawableImage);
tabLayout.getTabAt(i).setCustomView(imageView);
}
快乐编码
https://stackoverflow.com/questions/34237896
复制相似问题