• Android学习笔记 56. TabLayout 选项卡布局


    Android学习笔记

    Android基础开发——布局

    56. TabLayout 选项卡布局

    官方文档

    https://developer.android.google.cn/reference/com/google/android/material/tabs/package-summary

    56.1 简介

    TabLayout 提供了一个水平布局来显示选项卡。

    • Tab:TabLayout中的item,可以通过newTab()创建。
    • TabView:Tab的实例,是一个包含ImageView和TextView的线性布局。
    • TabItem:一种特殊的“视图”,在TabLayout中可以显式声明Tab
    56.2 TabItem

    TabItem 是一个特殊的“视图”,它允许您在布局中声明选项卡项。这个视图实际上并没有添加到 TabLayout,它只是一个允许设置选项卡项的文本、图标和自定义布局的虚拟视图。

    56.3 TabLayout布局

    TabLayout 提供了一个水平布局来显示选项卡。

    布局文件

    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <com.google.android.material.tabs.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:ignore="MissingConstraints">
    
            <com.google.android.material.tabs.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="选项卡1" />
    
            <com.google.android.material.tabs.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="选项卡2" />
    
            <com.google.android.material.tabs.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="选项卡3"
                />
    
        com.google.android.material.tabs.TabLayout>
    
    LinearLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    运行

    在这里插入图片描述

    56.4 结合ViewPager实现切换效果

    定义四个布局文件

    layout1.xml

    
    <androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="View1"
            android:textSize="30sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    androidx.constraintlayout.widget.ConstraintLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    其他三个雷同

    主布局文件

    activity_main.xml

    
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <androidx.viewpager.widget.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <com.google.android.material.tabs.TabLayout
                android:id="@+id/tab_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintBottom_toBottomOf="parent" />
    
        androidx.viewpager.widget.ViewPager>
    
    androidx.constraintlayout.widget.ConstraintLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    适配器

    MyAdapter.java

    package com.dingjiaxiong.mytablayout;
    
    import android.content.Context;
    import android.view.View;
    import android.view.ViewGroup;
    
    import androidx.annotation.NonNull;
    import androidx.annotation.Nullable;
    import androidx.viewpager.widget.PagerAdapter;
    
    import java.util.List;
    
    public class MyAdapter extends PagerAdapter {
    
        private List<View> listView;
    
        private List<String> listTabTitle;
    
        private Context context;
    
        public MyAdapter(List<View> listView, List<String> listTabTitle, Context context) {
            this.listView = listView;
            this.listTabTitle = listTabTitle;
            this.context = context;
        }
    
        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return listTabTitle.get(position);
        }
    
        @Override
        public int getCount() {
            return listView.size();
        }
    
        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            container.addView(listView.get(position),0);
            return listView.get(position);
        }
    
        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView(listView.get(position));
        }
    
        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    调用代码

    MainActivity.java

    package com.dingjiaxiong.mytablayout;
    
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.viewpager.widget.ViewPager;
    import androidx.viewpager2.widget.ViewPager2;
    
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    
    import com.google.android.material.tabs.TabLayout;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class MainActivity extends AppCompatActivity {
    
        private TabLayout tabLayout;
        private ViewPager viewpager;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            viewpager = findViewById(R.id.view_pager);
            tabLayout = findViewById(R.id.tab_layout);
    
    
            LayoutInflater layoutInflater = getLayoutInflater();
    
            View view1 = layoutInflater.inflate(R.layout.layout1, null);
            View view2 = layoutInflater.inflate(R.layout.layout2, null);
            View view3 = layoutInflater.inflate(R.layout.layout3, null);
            View view4 = layoutInflater.inflate(R.layout.layout4, null);
    
            List<View> viewList = new ArrayList<>();
    
            viewList.add(view1);
            viewList.add(view2);
            viewList.add(view3);
            viewList.add(view4);
    
            List<String> listTabTitle = new ArrayList<>();
            listTabTitle.add("选项卡1");
            listTabTitle.add("选项卡2");
            listTabTitle.add("选项卡3");
            listTabTitle.add("选项卡4");
    
            MyAdapter myAdapter = new MyAdapter(viewList,listTabTitle,this);
            viewpager.setAdapter(myAdapter);
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    运行

    在这里插入图片描述

  • 相关阅读:
    机器学习、深度学习、自然语言处理学习 NLP-RoadMap-996station GitHub鉴赏官
    linux:将进程切换到后台且不退出
    【使用 BERT 的问答系统】第 7 章 :BERT 模型的未来
    导入导出问题
    Docker
    大数据培训企业开发案例实时读取目录文件到HDFS案例
    视频汇聚/视频云存储/视频监控管理平台EasyCVR安全检查的相关问题及解决方法2.0
    一个案例搞懂工厂模式和单例模式
    AI原生应用速通指南
    数据链路层主要问题及源于课本的答案
  • 原文地址:https://blog.csdn.net/weixin_44226181/article/details/126328162