簡體   English   中英

如何在TabLayout中設置圖標大小?

[英]How to set icon size in 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]);

圖標的大小取決於圖像大小。 我怎么能調整它?

設置圖標填充

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

更改圖標大小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 

            }

為了完全控制大小,我建議使用自定義視圖來呈現選項卡。

首先創建一個新布局 - 此示例名為“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);
                }
            }
        }
}

你可以這樣做:

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

如果您不想將xml用於自定義視圖。 只需在運行時創建imageview,然后在tablayout中將其添加到特定選項卡中。

  ImageView imgView= new ImageView(MainActivity.this);
  imgView.setImageResource(drawableImage);
  imgView.setPadding(10,10,10,10)
  tabLayout.getTabAt(1).setCustomView(imgView);

它看起來像這樣。

在此輸入圖像描述

我決定關閉這個問題。 因為我們實際上無法在TabLayout中為圖標設置特定大小,而是填充它。 參考: https//developer.android.com/guide/practices/ui_guidelines/icon_design_tab

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM