底部導(dǎo)航TabLayout拉近圖標(biāo)與文字的距離

在使用TabLayout的時(shí)候,對(duì)于導(dǎo)航內(nèi)容的圖標(biāo)和文字設(shè)置,通常如下:

tabLayout = (TabLayout) findViewById(R.id.tab);
tabLayout.addTab(tabLayout.newTab().setText("關(guān)注").setIcon(R.drawable.ic_home_white_24dp));
tabLayout.addTab(tabLayout.newTab().setText("發(fā)現(xiàn)").setIcon(R.drawable.ic_data_usage_white_24dp));
tabLayout.addTab(tabLayout.newTab().setText("個(gè)人").setIcon(R.drawable.ic_person_white_24dp));

展示效果如下:


初始

然后就會(huì)發(fā)現(xiàn)圖標(biāo)與文字之間的距離過(guò)大,導(dǎo)致導(dǎo)航高度過(guò)長(zhǎng),看得實(shí)在是不太順眼 = =。如何調(diào)近它們之間的距離呢?這里介紹一個(gè)很簡(jiǎn)單的方法。
首先重寫一個(gè)圖標(biāo)+文字的布局tab_item_view.xml,大致如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textview"
        android:textSize="11dp"
        android:textColor="@color/colorBackground"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

接著在activity的Java文件里添加如下函數(shù):

// Tab自定義view
    public View getTabView(String title, int image_src) {
        View v = LayoutInflater.from(getApplicationContext()).inflate(R.layout.tab_item_view, null);
        TextView textView = (TextView) v.findViewById(R.id.textview);
        textView.setText(title);
        ImageView imageView = (ImageView) v.findViewById(R.id.imageview);
        imageView.setImageResource(image_src);
        return v;
    }

然后進(jìn)行調(diào)用:

tabLayout = (TabLayout) findViewById(R.id.tab);
tabLayout.addTab(tabLayout.newTab().setText("關(guān)注").setIcon(R.drawable.ic_home_white_24dp));
tabLayout.addTab(tabLayout.newTab().setText("發(fā)現(xiàn)").setIcon(R.drawable.ic_data_usage_white_24dp));
tabLayout.addTab(tabLayout.newTab().setText("個(gè)人").setIcon(R.drawable.ic_person_white_24dp));
// 修改樣式,主要是調(diào)近了圖標(biāo)與文字之間的距離
tabLayout.getTabAt(0).setCustomView(getTabView("關(guān)注",R.drawable.ic_home_white_24dp));
tabLayout.getTabAt(1).setCustomView(getTabView("發(fā)現(xiàn)",R.drawable.ic_data_usage_white_24dp));
tabLayout.getTabAt(2).setCustomView(getTabView("個(gè)人",R.drawable.ic_person_white_24dp));

可以在布局文件里將TabLayout控件的高度調(diào)小,最終效果如下,感覺(jué)是不是順眼多了?


改良后的樣式

對(duì)了,使用這個(gè)方法的話,要順便再修改一下移動(dòng)Tab時(shí)對(duì)應(yīng)的監(jiān)聽事件,大致如下:

tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
                changeTabSelect(tab);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                changeTabNormal(tab);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
});

// 切換顏色
    private void changeTabSelect(TabLayout.Tab tab) {
        View view = tab.getCustomView();
        ImageView img_title = (ImageView) view.findViewById(R.id.imageview);
        TextView txt_title = (TextView) view.findViewById(R.id.textview);
        txt_title.setTextColor(getResources().getColor(R.color.colorBase1));
        if (txt_title.getText().toString().equals("關(guān)注")) {
            img_title.setImageResource(R.drawable.ic_home_green_24dp);
        } else if (txt_title.getText().toString().equals("發(fā)現(xiàn)")) {
            img_title.setImageResource(R.drawable.ic_data_usage_green_24dp);
        }  else if (txt_title.getText().toString().equals("個(gè)人")) {
            img_title.setImageResource(R.drawable.ic_person_green_24dp);
        }
    }

    private void changeTabNormal(TabLayout.Tab tab) {
        View view = tab.getCustomView();
        ImageView img_title = (ImageView) view.findViewById(R.id.imageview);
        TextView txt_title = (TextView) view.findViewById(R.id.textview);
        txt_title.setTextColor(getResources().getColor(R.color.colorBackground));
        if (txt_title.getText().toString().equals("關(guān)注")) {
            img_title.setImageResource(R.drawable.ic_home_white_24dp);
        } else if (txt_title.getText().toString().equals("發(fā)現(xiàn)")) {
            img_title.setImageResource(R.drawable.ic_data_usage_white_24dp);
        }  else if (txt_title.getText().toString().equals("個(gè)人")) {
            img_title.setImageResource(R.drawable.ic_person_white_24dp);
        }
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容