美文网首页
android之TabLayout介绍

android之TabLayout介绍

作者: Lee_5566 | 来源:发表于2020-12-07 10:28 被阅读0次
    image.png

    TabLayout

    TabLayout是Android support中的一个控件android.support.design.widget.TabLayout,Google在升级了AndroidX之后,将TabLayout迁移到material包下面去了com.google.android.material.tabs.TabLayout,原来的support下面的TabLayout从API 29开始就不再维护了。

    所以如果项目已经升级了AndroidX,建议直接使用后者。

    TabLayout一般结合ViewPager+Fragment的使用实现滑动的标签选择器。

    实战

    activity_main.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <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">
    
        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="667dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.100000024"/>
    
            <androidx.viewpager.widget.ViewPager
                android:id="@+id/view_pager"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toBottomOf="@+id/tab_layout" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    MainActivity.java:

    package com.exmple.tabloayout;
    
    import androidx.annotation.NonNull;
    import androidx.annotation.Nullable;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.fragment.app.Fragment;
    import androidx.fragment.app.FragmentPagerAdapter;
    import androidx.viewpager.widget.ViewPager;
    
    import android.os.Bundle;
    
    import com.google.android.material.tabs.TabLayout;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class MainActivity extends AppCompatActivity {
        private String[] tabs = {"tab1", "tab2", "tab3"};
        private List<TabFragment> tabFragmentList = new ArrayList<>();
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            TabLayout tabLayout = findViewById(R.id.tab_layout);
            ViewPager viewPager = findViewById(R.id.view_pager);
            //添加tab
            for (int i = 0; i < tabs.length; i++) {
                tabLayout.addTab(tabLayout.newTab().setText(tabs[i]));
                tabFragmentList.add(TabFragment.newInstance(tabs[i]));
            }
    
            viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
                @NonNull
                @Override
                public Fragment getItem(int position) {
                    return tabFragmentList.get(position);
                }
    
                @Override
                public int getCount() {
                    return tabFragmentList.size();
                }
    
                @Nullable
                @Override
                public CharSequence getPageTitle(int position) {
                    return tabs[position];
                }
            });
    
            //设置TabLayout和ViewPager联动
            tabLayout.setupWithViewPager(viewPager,false);
        }
    }
    

    fragment_tab.xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/tv_bg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    TabFragment.java:

    package com.exmple.tabloayout;
    
    import android.graphics.Color;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    
    import androidx.annotation.NonNull;
    import androidx.annotation.Nullable;
    import androidx.fragment.app.Fragment;
    
    public class TabFragment extends Fragment {
        public static TabFragment newInstance(String label) {
            Bundle args = new Bundle();
            args.putString("label", label);
            TabFragment fragment = new TabFragment();
            fragment.setArguments(args);
            return fragment;
        }
    
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_tab, container, false);
        }
    
        @Override
        public void onStart() {
            super.onStart();
            String label = getArguments().getString("label");
            TextView text = getView().findViewById(R.id.tv_bg);
            text.setText(label);
            text.setBackgroundColor(Color.rgb((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random() * 255)));
        }
    }
    

    执行结果:


    image.png

    相关文章

      网友评论

          本文标题:android之TabLayout介绍

          本文链接:https://www.haomeiwen.com/subject/yjunsctx.html