2015-12-12 32 views
12

我正試圖增加我的應用程序中選項卡的圖標大小。圖標大小是固定的嘗試了很多方式,但沒有任何工作,最後嘗試了以下,但沒有改變的大小。請如果任何人可以告訴我正確的方式,我會很高興。提前感謝。如何增加TabLayout中選項卡的圖標大小

這裏是我的代碼,

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

回答

39

您好我之前遇到過同樣的問題,這是我最好的解決方案ULD發現:

您應該使用(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)); 

... 
+0

非常感謝。這是最好的解決方案:) – veena

+0

但我需要爲每個選項卡圖標創建不同的視圖 – veena

+0

歡迎您,我會爲您更新答案 –

11

如文檔中所述,您可以創建具有ImageView新的佈局android:id="@android:id/icon"並將其設置爲選項卡的自定義視圖。 TabLayout會自動將圖標放入內部ImageView中,並在您創建選項卡的同時撥打setIcon(R.drawable.yourIcon)

然後你就可以在自定義佈局應用您爲循環:

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> 
+3

爲我工作。此外,如果您想使用選擇器來表示不同的狀態,則需要設置自定義視圖的初始選定狀態。 。tab.getCustomView()的setSelected(tab.isSelected()); – lilienberg

+0

什麼是tab_icon_size? –

+0

這只是在values/dimens.xml文件中創建的[維度資源](https://developer.android.com/samples/BasicSyncAdapter/res/values/dimen.html)。 –

3

無需採取任何XML視圖。創建ImageView的運行時間,如果你想我的意思是完整的應用程序,那麼你可以試試這個所有屏幕內改變添加圖像視圖:)

for (int i = 0; i < tabLayout.getTabCount(); i++) { 
     ImageView imageView = new ImageView(context); 
     imageView.setImageResource(arr_drawable[i]); 
     tabLayout.getTabAt(i).setCustomView(imageView); 
    } 

快樂作弄

+0

是否有可能改變圖像視圖大小? – podgradle

+0

是的,您可以通過設置其參數運行時來更改它 –

1

: 創建佈局文件夾內的文件名爲:design_layout_tab_icon.xml 和粘貼如下代碼

<?xml version="1.0" encoding="utf-8"?> 
<ImageView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@android:id/icon" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="center_horizontal" 
     android:scaleType="fitCenter" 
     tools:src="@mipmap/ic_launcher"/> 

不要更改ID把它和你的屏幕裏的標籤欄設置一定高度使y你想和中提琴! 它適用於所有地方

相關問題