Andriod TabLayout

本文介绍了Android TabLayout的使用方法,包括如何引入库、基本使用、常用属性设置以及与ViewPager的配合使用。通过示例代码展示了如何创建Tab并自定义样式,同时强调了TabLayout在不同场景下的模式选择。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TabLayout

TabLayout是Android Design库中的实现选项卡效果的第三方控件,TabLayout是安卓6.0之后出现的。
1.引入库
添加依赖 implementation ‘com.android.support:design:28.0.0’
2.简单使用
2.1layout中添加控件

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.TabLayout
    android:id="@+id/tablayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabSelectedTextColor="#f3f000"
    app:tabGravity="fill"
    app:tabMode="fixed"
    app:tabTextColor="#000000"
    >
</android.support.design.widget.TabLayout>
2.2Activity代码

public class TabLayoutSimple extends AppCompatActivity {
private TabLayout mTablayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_table_layout_simple);

    mTablayout = (TabLayout) findViewById(R.id.tablayout);

// 添加选项
mTablayout.addTab(mTablayout.newTab().setText(“热门”));
mTablayout.addTab(mTablayout.newTab().setText(“最新”));
mTablayout.addTab(mTablayout.newTab().setText(“收藏”));
}
}

2.3TabLayout的常用属性
在布局中添加xmlns:app=“http://schemas.android.com/apk/res-auto”
// 下方横线指示器颜色
app:tabIndicatorColor=""
// 指示器高度,设置为0就是没有指示器
app:tabIndicatorHeight=""
// Tab文本默认颜色
app:tabTextColor=""
Tab文本被选中后的颜色
app:tabSelectedTextColor=""
//为Tab文本设置样式,一般是需要为Tab加图标时使用,也可以改变字体大小,定义style

app:tabTextAppearance=""
app:tabMode="" 只有两个值:fixed、scrollable
fixed用于标题栏少的情况,每个Tab可以平分屏幕宽度
scrollable用于标题栏多出屏幕的情况,如果标题栏少的时候用很难看,占不满屏幕

// TabLayout背景,和android:background="“效果一样
app:tabBackground=”"
// 对齐方式: 居中显示center、fill填满
app:tabGravity=""

// 从左边开始偏移距离,tabMode值必须为scrollable才会生效
app:tabContentStart=“150dp”

// 选项卡宽度限制
app:tabMaxWidth="" 最大宽度
app:tabMinWidth="" 最小宽度

// Tab内边距
app:tabPaddingStart=""
app:tabPaddingBottom=""
app:tabPaddingEnd=""
app:tabPaddingTop=""
app:tabPadding=""
3.TabLayout和ViewPager配合使用
(1)TabLayout的Tab数量由ViewPager分页数量决定
(2)TabLayout的Tab内容由ViewPager的Adapter中 getPagerTitle() 方法返回的内容决定

3.1layout中添加控件

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
app:tabGravity=“fill”
app:tabIndicatorColor="?attr/colorPrimary"
app:tabIndicatorHeight=“0dp”
app:tabMode=“scrollable”
app:tabSelectedTextColor="@color/red"
app:tabTextColor="#000000" />

<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width=“match_parent”
android:layout_height=“match_parent” />

3.2Activity代码
public class TabLayoutViewPager extends AppCompatActivity {
private String[] titles = new String[]{
“关注”,
“推荐”,
“热点”,
“南京”,
“两会”,
“视频”,
“新时代”,
“小视频”,
“图片”,
“问答”
};
private List fragments;
private TabLayout mTablayout;
private ViewPager mViewPager;
private MyPagerAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tab_layout_view_pager);
    // 初始化fragment数据
    initFragments();  
    // 初始化控件
    mTablayout = (TabLayout) findViewById(R.id.tablayout);
    mViewPager = (ViewPager) findViewById(R.id.vp);
    adapter = new MyPagerAdapter(getSupportFragmentManager());
    // viewPager设置适配器
    mViewPager.setAdapter(adapter);
    // TabLayout 和ViewPager设置绑定
    mTablayout.setupWithViewPager(mViewPager);
}

private void initFragments() {
    fragments = new ArrayList<>();
    for (int i = 0; i < titles.length; i++) {
        TabFragment fragment =new  TabFragment();
        Bundle bundle = new Bundle();
        bundle.putString("key",titles[i]);
        fragment.setArguments(bundle);
        fragments.add(fragment);
    }
}

/**
 * 必须要重写getPageTitle方法
 */

class MyPagerAdapter extends FragmentPagerAdapter{

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        return fragments.get(i);
    }

    @Override
    public int getCount() {
        return titles.length;
    }

    // 必须重写该方法
    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }
}

}

### Android TabLayout 使用指南 #### 创建项目并引入依赖项 为了使用 `TabLayout`,需要在项目的 `build.gradle` 文件中添加必要的依赖项。通常情况下,这涉及到 Material 组件库。 ```gradle dependencies { implementation 'com.google.android.material:material:<latest-version>' } ``` 确保替换 `<latest-version>` 为最新版本号[^1]。 #### 初始化 TabLayout 和 ViewPager2 (如果适用) 当设置带有视图分页功能的标签布局时,可以将 `TabLayout` 与 `ViewPager2` 结合起来使用。下面是一个简单的例子来展示如何初始化这两个组件: ```java // Java code snippet to initialize TabLayout with ViewPager2 mTabLayout = findViewById(R.id.tab_layout); mViewPager2 = findViewById(R.id.view_pager); MyPagerAdapter adapter = new MyPagerAdapter(this); mViewPager2.setAdapter(adapter); new TabLayoutMediator(mTabLayout, mViewPager2, (tab, position) -> tab.setText(titles[position]) ).attach(); ``` 这段代码展示了如何通过 `TabLayoutMediator` 将 `TabLayout` 连接到 `ViewPager2` 上,并设置了每个选项卡的文字内容。 #### 动态创建 Tabs 并移除默认指示器 对于动态生成多个 tabs 的情况,可以通过循环遍历数组或其他集合对象来进行操作。这里给出了一种方法,在不显示下划线的情况下添加文本到每一个新创建出来的 tab 中: ```java for (int i = 0; i < mTitles.length; ++i){ TabLayout.Tab tab = mTabLayout.newTab(); tab.setTag(i); tab.setText(mTitles[i]); mTabLayout.addTab(tab); } // 移除所有选中的状态下的颜色变化效果以及底部线条 mTabLayout.setSelectedTabIndicatorHeight(0); mTabLayout.setTabTextColors(ColorStateList.valueOf(Color.BLACK)); ``` 上述代码片段实现了无指示器样式的自定义化处理方式[^2]。 #### 完整的应用实例 最后提供了一个完整的应用类文件作为参考,该文件包含了基本的界面构建逻辑: ```java package com.example.myapp; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import com.google.android.material.tabs.TabLayout; public class MainActivity extends AppCompatActivity { private String[] mTitles = {"Home", "Profile", "Settings"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TabLayout mTabLayout = findViewById(R.id.tab_layout); for (String title : mTitles) { mTabLayout.addTab(mTabLayout.newTab().setText(title)); } // Customize appearance here... } } ``` 此段代码来自一个典型的 Android 应用程序入口点——活动(`Activity`) 类,其中包含了对 `TabLayout` 控件的基本配置[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值