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

首頁 > 系統 > Android > 正文

Android中TabLayout+ViewPager實現tab和頁面聯動效果

2019-10-22 18:35:10
字體:
來源:轉載
供稿:網友

TabLayout+ViewPager實現tab和頁面聯動效果

xml中:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android/243768.html">android/95442.html">android="http://schemas.android.com/apk/res/android"  xmlns:app="http://schemas.android.com/apk/res-auto"  android:id="@+id/activity_main"  android:layout_width="match_parent"  android:layout_height="match_parent">  <android.support.design.widget.TabLayout    android:id="@+id/toolbar_tl_tab"    android:layout_width="match_parent"    android:layout_height="?attr/actionBarSize"    android:layout_gravity="bottom"    app:layout_scrollFlags="scroll"    app:tabIndicatorColor="@android:color/holo_green_light"    app:tabSelectedTextColor="@android:color/holo_green_light" />  <View    android:layout_width="match_parent"    android:layout_height="1dp"    android:background="#f0f0f0" />  <android.support.v4.view.ViewPager    android:id="@+id/vp_container"    android:layout_width="match_parent"    android:layout_height="match_parent" /></RelativeLayout>

代碼中使用:

public class MainActivity extends AppCompatActivity {  private TabLayout toolbar_tl_tab;  private ViewPager vp_container;  private String[] titles = {"標題1", "標題2", "標題3", "標題4"};  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    init();  }  private void init() {    toolbar_tl_tab = (TabLayout) findViewById(R.id.toolbar_tl_tab);    vp_container = (ViewPager) findViewById(R.id.vp_container);    toolbar_tl_tab.setupWithViewPager(vp_container);    toolbar_tl_tab.setTabMode(TabLayout.MODE_SCROLLABLE);    vp_container.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {      @Override      public Fragment getItem(int position) {        return new PageFragment();      }      @Override      public CharSequence getPageTitle(int position) {        return titles[position];      }      @Override      public int getCount() {        return titles.length;      }    });  }}

碎片:PageFragment

public class PageFragment extends Fragment {  @Nullable  @Override  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {    View view = inflater.inflate(R.layout.fragment_page, null);    return view;  }}

碎片xml:fragment_page.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical">  <TextView    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center"    android:text="頁面" /></LinearLayout>

注意:

1、模式相關

使用滾動模式,特點是超過屏幕可以滾動顯示:

toolbar_tl_tab.setTabMode(TabLayout.MODE_SCROLLABLE);

使用屏幕等分模式,特點是顯示tab的寬度是屏幕等分后的寬度:

toolbar_tl_tab.setTabMode(TabLayout.MODE_FIXED);

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 镇坪县| 南阳市| 商都县| 四子王旗| 陇西县| 镇远县| 濮阳市| 蕲春县| 洛川县| 石楼县| 庐江县| 五河县| 来安县| 上饶县| 龙游县| 额济纳旗| 突泉县| 同心县| 九江市| 镇康县| 临武县| 蒙城县| 盐源县| 增城市| 四平市| 平乐县| 沽源县| 南昌市| 辰溪县| 桃园市| 聂荣县| 静宁县| 新营市| 昂仁县| 沛县| 中江县| 张家港市| 晋城| 安乡县| 渝中区| 晋州市|