精华内容
下载资源
问答
  • TabLayout

    2020-02-27 19:25:22
    TabLayout一.TabLayout介绍2.TabLayout的使用 一.TabLayout介绍 Tablayout继承自HorizontalScrollView,用作页面切换指示器,因使用简便功能强大而广泛使用在App中。 TabLayout可以做我们经常见到的那些头部的,...

    一.TabLayout介绍

    Tablayout继承自HorizontalScrollView,用作页面切换指示器,因使用简便功能强大而广泛使用在App中。

    TabLayout可以做我们经常见到的那些头部的,导航栏,点击导航栏的每一项可以切换不同的视图

    2.TabLayout的使用

    我们通过Fragment+ViewPager+TabLayout使其进行联动

    TabLayout 是属于 com.android.support:design 包的控件,所以需要依赖该包

    implementation 'com.android.support:design:28.0.0'
    

    在添加完依赖之后就可以在布局中直接引用了

    <?xml version="1.0" encoding="utf-8"?>
    <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"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
        <android.support.design.widget.TabLayout
            android:id="@+id/tbl"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            app:tabSelectedTextColor="@color/colorAccent">
    
        </android.support.design.widget.TabLayout>
    
        <android.support.v4.view.ViewPager
            android:id="@+id/vp"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="10">
    
        </android.support.v4.view.ViewPager>
    </LinearLayout>
    

    Activity中的代码

    public class MainActivity extends AppCompatActivity {
        private TabLayout tbl;
        private ViewPager vp;
        private List<Fragment> fragments = new ArrayList<>();
        private List<String> titles = new ArrayList<>();
        private MyAdapter myAdapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initDatas();
            initViews();
        }
    
        private void initDatas() {
            fragments.add(new OneFragment());
            fragments.add(new TwoFragment());
            fragments.add(new ThreeFragment());
    
            titles.add("标题1");
            titles.add("标题2");
            titles.add("标题3");
        }
    
        private void initViews() {
    
            tbl = (TabLayout) findViewById(R.id.tbl);
            vp = (ViewPager) findViewById(R.id.vp);
            myAdapter = new MyAdapter(getSupportFragmentManager(),fragments,titles);
    
            vp.setAdapter(myAdapter);
    
            tbl.setupWithViewPager(vp);//绑定
    
    
        }
    }
    

    在这里 需要将TabLayout与ViewPager相联结调用TabLayout的setupWithViewPager方法,参数为一个ViewPager

    ViewPager需要一个适配器

    public class MyAdapter extends FragmentPagerAdapter {
    
        private List<Fragment> fragments;
        private List<String> titles;
    
        public MyAdapter(FragmentManager fm, List<Fragment> fragments, List<String> titles) {
            super(fm);
            this.fragments = fragments;
            this.titles = titles;
        }
    
        @Override
        public Fragment getItem(int i) {
            return fragments.get(i);
        }
    
        @Override
        public int getCount() {
            return fragments.size();
        }
    
        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }
    }
    

    放置了三个普通的Fragment作展示
    这里只展示其中一个

    public class OneFragment extends Fragment {
    
    
        public OneFragment() {
            // Required empty public constructor
        }
    
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.fragment_one, container, false);
        }
    
    }
    
    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout 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"
        tools:context=".fragment.OneFragment">
    
        <!-- TODO: Update blank fragment layout -->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/hello_blank_fragment1" />
    
    </FrameLayout>
    

    最后进行效果展示
    在这里插入图片描述
    要开心加油

    展开全文
  • tablayout

    2018-06-04 15:39:19
    1.依赖包: compile 'com.android.support:design:23.3.0'... 2.在xml中写控件 ...android.support.design.widget.TabLayout ...3.设置tablayout的内容 ... tablayout.addTab(tlTablayoutHome.newTab().setText("个性推...

    1.依赖包:

     

    compile 'com.android.support:design:23.3.0'

    2.在xml中写控件

     

    android.support.design.widget.TabLayout

    是这种的。

     

    3.设置tablayout的内容 

       tablayout.addTab(tlTablayoutHome.newTab().setText("个性推荐"));

    4.设置tablayout字体下面横线的颜色 

    tlTablayoutHome.setSelectedTabIndicatorColor(ContextCompat.getColor(getActivity(),
     R.color.red));
    
    
    5.设置tablayout字体颜色
            tlTablayoutHome.setTabTextColors(ContextCompat.getColor(getActivity(), R.color.black), 
    ContextCompat.getColor(getActivity(), R.color.red));
    

     

    6.tablayout的监听

           tablayout.setOnTabSelectListener()

    7.tablayout点击去掉阴影

    app:tabRippleColor="@color/transparent"

    8.下划线和字体长度一样

    app:tabIndicatorFullWidth="false"

    9.改变选中字体的颜色

    app:tabSelectedTextColor="@color/purple"

    10.改变指示器下标的颜色

    app:tabIndicatorColor="@android:color/holo_orange_light"

    11.改变未选中字体的颜色

    app:tabTextColor="@color/colorPrimary"

    展开全文
  • Tablayout

    2018-11-17 10:58:09
    1—添加依赖 ...2—添加布局 ...android.support.design.widget.TabLayout android:id="@+id/tablayout" android:layout_width="wrap_content" android:layout_height="w...

    1—添加依赖

    compile ‘com.android.support:design:28+’

    2—添加布局

    <android.support.design.widget.TabLayout
        android:id="@+id/tablayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />
    
    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v4.view.ViewPager>
    

    3-- MainActivity中逻辑处理

    public class MainActivity extends AppCompatActivity {

    private TabLayout tablayout;
    private ViewPager view_pager;
    ArrayList<Fragment> alist=new ArrayList<Fragment>();
    private Myadapter madapter;
    
    ArrayList<String> titlelist=new ArrayList<String>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tablayout = findViewById(R.id.tablayout);
        view_pager = findViewById(R.id.view_pager);
    
        /*
        //先写入数据
        initData();
        //适配器
        MPagerAdapter mPagerAdapter=new MPagerAdapter(getSupportFragmentManager());
        viewpager.setAdapter(mPagerAdapter);
        //tablayout的显示模式   MODE_FIXED固定的 0    MODE_SCROLLABLE可滑动的
        tablayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        //让tablayout和viewpager关联起来  一起动
        tablayout.setupWithViewPager(viewpager);
         */
    
        madapter = new Myadapter(getSupportFragmentManager());
        view_pager.setAdapter(madapter);
        //在适配器下面  就要刷新
        initData();
        tablayout.setTabMode(0);
        tablayout.setupWithViewPager(view_pager);
    }
    
    private void initData() {
        for (int i = 0; i < 11; i++) {
            titlelist.add("标题"+i);
            alist.add(new MFragment());
        }
        madapter.notifyDataSetChanged();//如果initData()在 适配器下面 就刷新
    }
    
    private class Myadapter extends FragmentPagerAdapter {
        public Myadapter(FragmentManager fm) {
            super(fm);
        }
    
        @Override
        public Fragment getItem(int i) {
            return alist.get(i);
        }
    
        @Override
        public int getCount() {
            return alist.size();
        }
        //返回页面的标题
        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return titlelist.get(position);
        }
    }
    

    }

    4.MFragment 展示随机数

    public class MFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    TextView textView=new TextView(getActivity());
    double random = Math.random();
    textView.setText(“111111”+random);
    return textView;
    }
    }

    展开全文
  • tabLayout

    2017-11-23 08:25:01
    gradle中添加依赖 *compile'... ... * 1--写tablayout * 2--- 写viewPager * 3-- 写viewPagerAdapter * 4-- 让TabLayout和Viewpager关联; */ 布局文件代码 xml version="1.0" encoding

    gradle中添加依赖

    *compile'com.android.support:design:26+'

    /**
     * 1--写tablayout
     * 2--- 写viewPager
     * 3-- 写viewPagerAdapter
     * 4-- 让TabLayout和Viewpager关联;
     */
    
    
    布局文件代码
    	
    <?xml version="1.0" encoding="utf-8"?>
    <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"
        android:orientation="vertical"
        tools:context="com.bwie.tablayoutdemo2.MainActivity">
    
        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    
    
    </LinearLayout>
    
    
    --------------------------
    MainActivity代码,  注意FragmentPagerAdapt中要重写getPageTitle方法;
    	
    package com.bwie.tablayoutdemo2;
    
    import android.os.Bundle;
    import android.support.design.widget.TabLayout;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentPagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.support.v7.app.AppCompatActivity;
    
    import java.util.ArrayList;
    
    /**
     * 1--tablayout
     * 2--- viewPager
     * 3--viewPagerAdapter
     * 4-- 让TabLayout和Viewpager关联;
     */
    public class MainActivity extends AppCompatActivity {
    
        private TabLayout tabLayout;
        private ViewPager viewPager;
        ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
        ArrayList<String> titleList = new ArrayList<String>();
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            tabLayout = (TabLayout) findViewById(R.id.tabLayout);
            viewPager = (ViewPager) findViewById(R.id.viewPager);
            initData();
            //ctrl+p 提示参数的快捷键
            MPagerAdapter mPagerAdapter = new MPagerAdapter(getSupportFragmentManager());
            viewPager.setAdapter(mPagerAdapter);
    
            //设置TabLayout的模式
            tabLayout.setTabMode(TabLayout.MODE_FIXED);
            //让tablayout和Viewpager关联;
            tabLayout.setupWithViewPager(viewPager);
    
        }
    
        private void initData() {
            for (int i = 0; i <4 ; i++) {
                fragmentList.add(new BlankFragment());
                titleList.add("爱因斯坦" +i);
            }
        }
    
    
        class  MPagerAdapter extends FragmentPagerAdapter{
    
            public MPagerAdapter(FragmentManager fm) {
                super(fm);
            }
    
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }
    
            @Override
            public int getCount() {
                return fragmentList.size();
            }
    
            //需要重写个返回标题的方法;
            @Override
            public CharSequence getPageTitle(int position) {
                return titleList.get(position);
            }
        }
    }
    
    ------------------------------------------
    Fragment代码,Fragment用的都是V4包里的;
    public class BlankFragment extends Fragment {
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            TextView textView = new TextView(getActivity());
            double random = Math.random();
            textView.setText("xxxxxxxxxxx"+random);
            return textView;
        }
    }
    展开全文

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 16,908
精华内容 6,763
关键字:

tablayout