国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

TabLayout 解析

2019-11-09 20:41:09
字體:
供稿:網(wǎng)友

1. 簡介

我們在應(yīng)用viewpager的時候,經(jīng)常會使用TabPageIndicator來與其配合,達到很漂亮的效果。

但是TabPageIndicator是第三方的,而且比較老了,當(dāng)然了現(xiàn)在很多大神都已經(jīng)開始自己寫TabPageIndicator來滿足自己的需求

在2015年的google大會上,google發(fā)布了新的Android Support Design庫,里面包含了幾個新的控件,其中就有一個TabLayout,它就可以完成TabPageIndicator的效果,而且還是官方的,最好的是它可以兼容到2.2以上版本,包括2.2

這里寫圖片描述

2. 使用指南與Demo

2.1 添加依賴

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

2.2 xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <include layout="@layout/titlebar"/> <android.support.design.widget.TabLayout android:id="@+id/tabLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/holo_blue_bright" app:tabIndicatorColor="@android:color/holo_red_dark" app:tabSelectedTextColor="@android:color/holo_red_dark" app:tabTextColor="@android:color/white" /> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" /></LinearLayout>

2.3 定義和創(chuàng)建Fragment

public class MyTabFragment extends Fragment { //標題 PRivate final String title; //內(nèi)容 private final String content; /** * 得到內(nèi)容 * @return */ public String getContent() { return content; } /** * 得到標題 * @return */ public String getTitle() { return title; } Context mContext; TextView textView; public MyTabFragment(String title, String content){ super(); this.title = title; this.content = content; } @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); //上下文 mContext = getContext(); } /** * 創(chuàng)建視圖 * @param inflater * @param container * @param savedInstanceState * @return */ @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { textView = new TextView(mContext); textView.setTextColor(Color.RED); textView.setTextSize(25); textView.setGravity(Gravity.CENTER); return textView; } /** * 綁定數(shù)據(jù),傳過來的數(shù)據(jù),在構(gòu)造方法中 * @param savedInstanceState */ @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); //設(shè)置內(nèi)容 textView.setText(content); }}

2.4 設(shè)置 ViewPager 適配器

public class ViewPagerAdapter extends FragmentPagerAdapter { private final ArrayList<MyTabFragment> fragments; public ViewPagerAdapter(FragmentManager fm, ArrayList<MyTabFragment> fragments) { super(fm); this.fragments = fragments; } /** * 根據(jù)位置返回對應(yīng)的Fragment * @param position * @return */ @Override public Fragment getItem(int position) { return fragments.get(position); } @Override public int getCount() { return fragments.size(); } /** * 得到頁面的標題 * @param position * @return */ @Override public CharSequence getPageTitle(int position) { return fragments.get(position).getTitle(); }}

2.5 設(shè)置 TabLayout

//布局設(shè)置<android.support.design.widget.TabLayout android:id="@+id/tabLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/holo_blue_bright" app:tabIndicatorColor="@android:color/holo_red_dark" app:tabSelectedTextColor="@android:color/holo_red_dark" app:tabTextColor="@android:color/white" />//代碼//設(shè)置TabLayouttabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);tabLayout.setupWithViewPager(viewpager);//public class TabLayoutMainActivity extends AppCompatActivity { ViewPager viewPager; TextView tv_title; TabLayout tabLayout; ArrayList<MyTabFragment> fragments; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_tab_layout_main); viewPager = (ViewPager) findViewById(R.id.viewPager); tv_title = (TextView) findViewById(R.id.tv_title); tabLayout = (TabLayout) findViewById(R.id.tabLayout); tv_title.setText("TabLayout的使用"); //初始化數(shù)據(jù) fragments = new ArrayList<>(); for (int i=0;i<12;i++){ fragments.add(new MyTabFragment("標題 "+i,"內(nèi)容 "+i)); } viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(),fragments)); //關(guān)聯(lián)ViewPager tabLayout.setupWithViewPager(viewPager); //設(shè)置固定的// tabLayout.setTabMode(TabLayout.MODE_FIXED); tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); }}

3. 適用小結(jié)

此方法用于顯示標題

@Override public CharSequence getPageTitle(int position) { return fragments.get(position).getTitle(); }

關(guān)聯(lián)和顯示模式

tabLayout.setupWithViewPager(viewpager);tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

xml中自定義樣式(顏色)


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 汽车| 曲阜市| 班玛县| 顺平县| 闽清县| 邢台市| 西青区| 平罗县| 宜丰县| 仙桃市| 阆中市| 尼木县| 万载县| 新乡县| 体育| 郴州市| 平阴县| 钟祥市| 惠安县| 余姚市| 赞皇县| 湘阴县| 双辽市| 邵阳县| 桂阳县| 民权县| 武冈市| 习水县| 嘉峪关市| 丹东市| 巩留县| 尼勒克县| 西和县| 广水市| 沙河市| 浙江省| 沂南县| 仁寿县| 沂南县| 天长市| 义乌市|