2016-01-08 2 views
7

Вот мой код:Как установить размер значка в TabLayout?

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

Размер иконы в зависимости от размера изображения. Как я могу изменить его размер?

+0

изменение высота и ширина от xml .. –

+0

попробуйте изменить размер с помощью редакторов –

+0

установить высоту tablayout для увеличения размера значка –

ответ

6

набор иконок обивка

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

Какая черта на земле - tablayout.getTabWidget()? Кажется, что их нет? –

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 

      } 
+1

У меня есть NPE в этой строке: 'view_bottom_tabLayout.getChildAt (i) .setLayoutParams (params);' –

+0

уверен, что вы используете этот tabLayout ('android.support.design.widget.TabLayout') в xml? и убедитесь, что вы добавили дочерние элементы в представление и используете эту строку после. – SaravanaRaja

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

вы можете сделать это:

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); 
      } 
     } 
    } 
Смежные вопросы