Android APP开发TabLayout控制的多样应用

一、XML静态设置TabItem:

<com.google.android.material.tabs.TabLayout
    android:id="@+id/tabLayout"
    android:layout_width="409dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="32dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="32dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <com.google.android.material.tabs.TabItem
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="北京" />

    <com.google.android.material.tabs.TabItem
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="上海" />

    <com.google.android.material.tabs.TabItem
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="广州" />
    <com.google.android.material.tabs.TabItem
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="深圳" />
</com.google.android.material.tabs.TabLayout>

MainActivity.java添加代码:

tabLayout1 = findViewById(R.id.tabLayout);
        textView1 = findViewById(R.id.textView);

        tabLayout1.getTabAt(0).select();
        textView1.setText("北京");

        tabLayout1.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tabLayout1.getSelectedTabPosition();

                switch (position){
                    case 0:
                        textView1.setText("北京");
                        break;
                    case 1:
                        textView1.setText("上海");
                        break;
                    case 2:
                        textView1.setText("广州");
                        break;
                    case 3:
                        textView1.setText("深圳");
                        break;
                    default:
                        break;
                }
            }

运行效果:

二、动态设置TabItem:

1)、xml代码:

<com.google.android.material.tabs.TabLayout
        android:id="@+id/tabLayout2"
        android:layout_width="409dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="32dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </com.google.android.material.tabs.TabLayout>
java部分代码:

tabLayout11 = findViewById(R.id.tabLayout2);
imageView11 = findViewById(R.id.imageView);

tabLayout11.addTab(tabLayout11.newTab().setText("杭州"));
tabLayout11.addTab(tabLayout11.newTab().setText("苏州"));
tabLayout11.addTab(tabLayout11.newTab().setText("武汉"));
tabLayout11.addTab(tabLayout11.newTab().setText("重庆"));

运行效果:

2)、java部分代码:

tabLayout21 = findViewById(R.id.tabLayout3);
        textView21 = findViewById(R.id.textView4);

        for (int i = 0; i < 18; i++) {
            list.add("tab" + i +"");
            tabLayout21.addTab(tabLayout21.newTab().setText(list.get(i)));
        }

        tabLayout21.getTabAt(0).select();
        textView21.setText(list.get(0));

        tabLayout21.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tabLayout21.getSelectedTabPosition();
                textView21.setText(list.get(position));
            }

运行效果:

三、完整工程:

AndroidAPP开发TabLayout控制的多样应用资源-CSDN文库

相关推荐

  1. 基于mqtt物联网控制移动应用程序开发

    2023-12-28 11:38:06       14 阅读
  2. 循环控制语句实际应用(2)

    2023-12-28 11:38:06       13 阅读
  3. 循环控制语句实际应用(3)

    2023-12-28 11:38:06       11 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-28 11:38:06       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-28 11:38:06       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-28 11:38:06       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-28 11:38:06       18 阅读

热门阅读

  1. el-select可输入下拉框限制长度

    2023-12-28 11:38:06       32 阅读
  2. vue 预览 pdf、word、excel

    2023-12-28 11:38:06       41 阅读
  3. Vue 3 中安装并使用 Axios 详细步骤+样例代码详解

    2023-12-28 11:38:06       31 阅读
  4. Ajax

    2023-12-28 11:38:06       29 阅读
  5. PDF.js介绍以及使用

    2023-12-28 11:38:06       38 阅读
  6. PDF是什么格式的文件

    2023-12-28 11:38:06       37 阅读
  7. Unity相机跟随角色移动

    2023-12-28 11:38:06       33 阅读
  8. C# JsonString转Object以及Object转JsonString

    2023-12-28 11:38:06       34 阅读
  9. C++ list的模拟实现

    2023-12-28 11:38:06       30 阅读