Android---TabLayout

目录

TabLayout

TabItem

​编辑

演示效果的xml

TabLayout

TabLayout 在开发中一般作为选项卡使用,常与 ViewPager2 和 Fragment 结合起来使用。

    常用属性:

    \bullet app:tabBackground 设置 TabLayout 的背景色,改变整个TabLayout 的颜色;

    \bullet app:tabTextColor 设置未被选中时文字的颜色;

    \bullet app:tabSelectorColor 设置选中时文字颜色;

    \bullet​​​​​​​ app:tabTextAppearance="@android:style/TextAppearance.Large" 设置 TabLayout 的文本主题,无法通过 textSize 来设置文字大小,只能通过主题来设定;

    \bullet​​​​​​​ app:tabMode="scrollable"设置 TabLayout 可滑动,当 tabItem 个数较多时,一个界面无法呈现所有的导航标签,此时就必须要用;

    \bullet​​​​​​​ app:tabIndicator 设置指示器;

    \bullet​​​​​​​ app:tabIndicatorColor 设置指示器颜色;

    \bullet​​​​​​​ app:tabIndecatorHeight 设置指示器高度,当 app:tabIndecatorHeight="0dp",隐藏 Indicator 效果;

    \bullet app:tabTextAppearance="@android:style/TextAppearance.Holo.Large" 改变 TabLayout 里 TabItem 文字的大小;

    \bullet app: tabPadding 设置 Tab 内部 item 的 padding。也可以单独设置某个方向的padding, 比如 app:tabPaddingStart 设置左边距;

    \bullet app:paddingEdng / app:paddingStart 设置整个 TabLayout 的 padding;

    \bullet app:tabGravity="center" 居中,如果是 fill,则充满;

    \bullet app:tabMaxWidth / app:tabMinWidth 设置最大/最小的 tab 宽度,对 Tab 的宽度进行限制。

TabItem

 给TabLayout 添加 Item 有两种方法,其中一种就是使用 TabItem 在 xml 里直接添加。

    1. 使用TabItem 给 TabLayout 添加卡片。

    \bullet android:icon 设置图标;

    \bullet Android:text 设置文本;

    2. 通过代码添加。使用 TabLayoutMediator()

        new TabLayoutMediator(binding.tab, binding.viewPager, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                //TODO 设置卡片的文本/图标
                tab.setText(mTitles.get(position))
                   .setIcon(mIcons.get(position));
            }
        }).attach();

其中 mTitles 和 mIcons 是存放 text 和 Icon 的list。效果如下:

可以看到 text 在英文状态下默认都是大写, 这是因为在 TabLayout 的源码中默认设置属性 textAllCaps=true。所以可以在 TabLayout 中设置如下属性来改成小写。

app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget"

 

演示效果的xml




    

        
        
        
    

    

        
        
        
        
        
        

    

    

        
        
        
    

    

        

        

        

    
    

        
        
        
    

你可能感兴趣的:(#,Material,Design,android)