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

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

TabLayout 解析

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

1. 簡介

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

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

在2015年的google大會(huì)上,google發(fā)布了新的Android Support Design庫,里面包含了幾個(gè)新的控件,其中就有一個(gè)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 { //標(biāo)題 PRivate final String title; //內(nèi)容 private final String content; /** * 得到內(nèi)容 * @return */ public String getContent() { return content; } /** * 得到標(biāo)題 * @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(); } /** * 得到頁面的標(biāo)題 * @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("標(biāo)題 "+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é)

此方法用于顯示標(biāo)題

@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ā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 朝阳县| 门源| 迭部县| 松潘县| 菏泽市| 余庆县| 纳雍县| 威海市| 昌江| 余姚市| 普格县| 康平县| 电白县| 昔阳县| 永清县| 正镶白旗| 呼和浩特市| 芦溪县| 舟山市| 宿迁市| 南城县| 龙胜| 高要市| 岳西县| 临江市| 通州市| 韩城市| 肥西县| 大渡口区| 睢宁县| 合水县| 南宁市| 宁夏| 太仆寺旗| 华安县| 黔南| 三门峡市| 贡山| 遂宁市| 奉化市| 清镇市|