之前都習慣用RadioGroup配合ViewPager做頁面切換,官方是我TabLayout倒是用的很少,記錄下使用。
布局:
<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"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorapp:tabIndicatorColor=”#0835f8” //該屬性下方滾動條顏色 app:tabIndicatorHeight=”2.0dp” //滾動條高度viewpager中我們使用已經定義的behavior來關聯Tablayout app:layout_behavior=”@string/appbar_scrolling_view_behavior”
<string name="appbar_scrolling_view_behavior" translatable="false">android.support.design.widget.AppBarLayout$ScrollingViewBehavior</string>代碼中:
toolbar_tab = (TabLayout) findViewById(R.id.toolbar_tab); main_vp_container = (ViewPager) findViewById(R.id.main_vp_container); PaletteViewPagerAdapter vpAdapter = new PaletteViewPagerAdapter(getSupportFragmentManager(), this); main_vp_container.setAdapter(vpAdapter); toolbar_tab.setupWithViewPager(main_vp_container);//關聯效果圖: 
新聞熱點
疑難解答