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

首頁 > 系統(tǒng) > Android > 正文

Android使用TabLayout+Fragment實(shí)現(xiàn)頂部選項(xiàng)卡

2019-12-12 02:51:59
字體:
供稿:網(wǎng)友

先看效果圖:

使用Tablayout,首先需要在項(xiàng)目中加入Design包

dependencies {  compile 'com.android.support:design:24.1.1' }

在activity_main.xml布局文件中

<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="zhengliang.com.tablayout.MainActivity">  <android.support.design.widget.TabLayout    android:id="@+id/tab"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:background="@color/colorPrimary"    >  </android.support.design.widget.TabLayout>  <android.support.v4.view.ViewPager    android:id="@+id/pager"    android:layout_width="match_parent"    android:layout_height="0dp"    android:layout_weight="1" /></LinearLayout>

里面一個(gè)Tablayout和一個(gè)ViewPager就行了

實(shí)現(xiàn)上面的效果需要?jiǎng)?chuàng)建幾個(gè)Fragment,這里為了方便就直接創(chuàng)建了一個(gè)基本的Fragment需要的時(shí)候直接new就行了,實(shí)現(xiàn)代碼如下:

public class BlankFragment extends Fragment {  public BlankFragment() {  }  public static BlankFragment newInstance(String text){    Bundle bundle = new Bundle();    bundle.putString("text",text);    BlankFragment blankFragment = new BlankFragment();    blankFragment.setArguments(bundle);    return blankFragment;  }  @Override  public View onCreateView(LayoutInflater inflater, ViewGroup container,               Bundle savedInstanceState) {    return inflater.inflate(R.layout.fragment_blank, container, false);  }  @Override  public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {    super.onViewCreated(view, savedInstanceState);    TextView textView = (TextView) view.findViewById(R.id.pager_text);    textView.setText(getArguments().getString("text"));  }}

有了Fragment還需要一個(gè)實(shí)現(xiàn)一個(gè)ViewPagerAdapter

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

Adapter的寫法非常簡(jiǎn)單,在自定義Adapter的時(shí)候需要重寫里面的getPagerTitle()方法,實(shí)現(xiàn)這個(gè)方法是為了當(dāng)Tablayout與ViewPager綁定的時(shí)候能夠綁定Tab標(biāo)簽的標(biāo)題

一切準(zhǔn)備就緒,直接看MainActivity.java中的代碼

public class MainActivity extends AppCompatActivity {  private TabLayout tab;  private ViewPager pager;  private List<String> list;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    /*初始化界面*/    initViews();    /*初始化數(shù)據(jù)*/    initData();    /*設(shè)置Adapter*/    pager.setAdapter(new MyAdapter(getSupportFragmentManager(),list));    /*Tab與ViewPager綁定*/    tab.setupWithViewPager(pager);  }  /*初始化數(shù)據(jù)*/  private void initData() {    list = new ArrayList<>();    for (int i = 0; i < 5 ; i++) {      list.add(String.format(Locale.CHINA,"第%02d頁",i));    }  }  /*初始化界面*/  private void initViews() {    this.pager = (ViewPager) findViewById(R.id.pager);    this.tab = (TabLayout) findViewById(R.id.tab);  }}

到這里基本就實(shí)現(xiàn)了上面圖的效果,里面一句很關(guān)鍵的代碼:tab.setupWithViewPager(pager);只有加了這句代碼才能實(shí)現(xiàn)Tab和ViewPager的綁定聯(lián)動(dòng)...

下面介紹一些TabLayout常用的屬性:

設(shè)置Tab標(biāo)簽的默認(rèn)字體顏色:

app:tabTextColor="#ddd"

設(shè)置Tab標(biāo)簽選中的字體顏色:

app:tabSelectedTextColor="@color/colorAccent"

設(shè)置指示器的顏色:

app:tabIndicatorColor="@color/colorAccent"

設(shè)置指示器的高度: (當(dāng)值為0dp的時(shí)候指示器為隱藏狀態(tài))

app:tabIndicatorHeight="5dp"

Tablayout中一個(gè)重要的屬性: app:tabMaode

該屬性有兩個(gè)值:### scrollable,fixed

"fixed"

固定的,表示Tab標(biāo)簽不管為多少都是固定的,所以當(dāng)標(biāo)簽很多的時(shí)候就會(huì)擠在一起,而且回造成標(biāo)簽上的文字顯示不完整:

如下圖:

app:tabMode="fixed"

scrollable

可滾動(dòng)的,當(dāng)Tab的標(biāo)簽超出屏幕的寬度,就會(huì)自動(dòng)出現(xiàn)課滑動(dòng)的效果,當(dāng)標(biāo)簽過多的時(shí)候還可以直接滑動(dòng)標(biāo)簽

app:tabMode="scrollable"

如下圖:

好了,上面這些就是TabLayout最基本的用法...

以上所述是小編給大家介紹的Android使用TabLayout+Fragment實(shí)現(xiàn)頂部選項(xiàng)卡,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林網(wǎng)網(wǎng)站的支持!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 浦城县| 延安市| 武威市| 赤峰市| 疏附县| 循化| 双鸭山市| 武汉市| 沾化县| 平阴县| 阜城县| 泗洪县| 安福县| 缙云县| 周宁县| 鄱阳县| 盘山县| 麻阳| 丰顺县| 乐昌市| 大港区| 榆林市| 洪雅县| 巴彦县| 阿拉善右旗| 赞皇县| 如东县| 桑植县| 泰来县| 庄河市| 牙克石市| 石柱| 陆河县| 郯城县| 肥西县| 株洲市| 宜君县| 鲜城| 五寨县| 四平市| 锡林郭勒盟|