How to create app bar with icons using TabLayout Android Design?

前端 未结 4 1249
小鲜肉
小鲜肉 2021-01-31 10:33

I\'m trying to use the new TabLayout in the android design library to create app bar with icons.

public void setupTabLayout(TabLayout tabLayout) {
    tabLayout         


        
4条回答
  •  眼角桃花
    2021-01-31 11:12

    From the documentation :

    https://developer.android.com/reference/android/support/design/widget/TabLayout.Tab.html#setCustomView(android.view.View)

    Set a custom view to be used for this tab. This overrides values set by setText(CharSequence) and setIcon(Drawable).

    you will have to set the title values yourself

    From your example:

    public void setupTabLayout(TabLayout tabLayout) {
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        tabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);
        tabLayout.setupWithViewPager(mViewpager);
    
        TextView tab = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tab.setText("Library");
        tab.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_tabbar_library, 0, 0);
        tabLayout.getTabAt(0).setCustomView(tab);
        //..
    }
    

    custom_tab.xml

    
    
    

    Update

    The api has changed to allow you to set a custom id so you don't have to set the text/drawable manually. It'll use the adapter's values.

    If the provided view contains a TextView with an ID of text1 then that will be updated with the value given to setText(CharSequence). Similarly, if this layout contains an ImageView with ID icon then it will be updated with the value given to setIcon(Drawable).

提交回复
热议问题