日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

TabLayout 与 FragmentTabHost

發布時間:2023/12/15 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 TabLayout 与 FragmentTabHost 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

為什么80%的碼農都做不了架構師?>>> ??


TabLayout 與 FragmentTabHost

Android提供實現Tab樣式的控件大致有TabActivity、FragmentTabHost、TabLayout。而TabActivity已經過時,這里就不在多說,主要提 一下Tablayout與FragmentTabHost這兩個

FragmentTabHost針對Fragment管理來進行界面切換,FragmentTabHost本身提供FragmentManager來管理Fragment。
TabLayout則傾向與ViewPager配合使用,可以支持手勢來切換界面。也可以模仿FragmentTabHost利用Fragment來管理界面切換

FragmentTabHost:
?布局代碼:
?<!--TabHost布局-->
?<?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"
???? >
???? <!--Toolbar-->
???? <include
???????? android:id="@+id/title_bar"
???????? android:layout_width="match_parent"
???????? android:layout_height="wrap_content"
???????? layout="@layout/toolbar_view"
???????? />
???? <!--Fragment 容器-->
???? <FrameLayout
???????? android:id="@+id/main_tab_host_context"
???????? android:layout_width="match_parent"
???????? android:layout_height="0dp"
???????? android:layout_weight="1.0"/>
???? <!--Tab與Fragment分割線-->
???? <View
???????? android:layout_width="match_parent"
???????? android:layout_height="2px"
???????? android:background="@color/divider_line_color"
???????? />
???? <!--Tab布局-->
???? <android.support.v4.app.FragmentTabHost
???????? android:layout_marginTop="4dp"
???????? android:id="@+id/bottom_tab_host"
???????? android:layout_width="match_parent"
???????? android:layout_height="wrap_content" />
?</LinearLayout>

?<!--Indicator布局-->
?<?xml version="1.0" encoding="utf-8"?>
?<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
???? android:layout_width="wrap_content"
???? android:layout_height="wrap_content"
???? android:gravity="center"
???? android:orientation="vertical"
???? >
???? <ImageView
???????? android:id="@+id/indicator_icon"
???????? android:layout_width="@dimen/main_tab_width"
???????? android:layout_height="@dimen/main_tab_height"
???????? android:src="@mipmap/apple"
???????? android:layout_gravity="center"
???????? />
???? <TextView
???????? android:id="@+id/indicator_text"
???????? android:layout_width="wrap_content"
???????? android:layout_height="match_parent"
???????? android:text="@string/Apple"
???????? android:textColor="@color/tab_text_default_color"
???????? android:layout_gravity="center"
???????? />
?</LinearLayout>

?Java代碼
?//初始化Tab
?private void initTab() {
??//獲取TabHost
??????? FragmentTabHost mFragmentTabHost = (FragmentTabHost) findViewById(R.id.bottom_tab_host);
??????? //該方法必須調用,用于初始化FragmentTabHost
??????? mFragmentTabHost.setup(TabActivity.this, getSupportFragmentManager(), R.id.main_tab_host_context);
??????? TabEnum[] tabEnums = TabEnum.values();
??????? for(TabEnum tabEnum : tabEnums) {
??????? ?//初始化Indicator
??????????? View indicator = LayoutInflater.from(getApplicationContext()).inflate(R.layout.indicator_tab, null);
??????????? //設置顯示文本
??????????? TextView tv = (TextView) indicator.findViewById(R.id.indicator_text);
??????????? tv.setText(getResources().getString(tabEnum.getName()));
??????????? //設置顯示圖標
??????????? ImageView iv = (ImageView) indicator.findViewById(R.id.indicator_icon);
??????????? iv.setImageResource(tabEnum.getIcon());
??????????? //創建Tab,并設置Indicator
??????????? TabHost.TabSpec tabSpec= mFragmentTabHost.newTabSpec(getResources().getString(tabEnum.getName())).setIndicator(indicator);
??????????? //添加Tab到TabHost
??????????? mFragmentTabHost.addTab(tabSpec, tabEnum.getClz(), null);
??????? }
??? }


TabLayout:
?布局代碼:
?<?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"
???? >

???? <android.support.design.widget.TabLayout
???????? android:id="@+id/top_tab"
???????? android:layout_width="match_parent"
???????? android:layout_height="wrap_content" />

???? <android.support.v4.view.ViewPager
???????? android:id="@+id/tab_viewpager"
???????? android:layout_width="match_parent"
???????? android:layout_height="match_parent" />

?</LinearLayout>

?Java代碼:
?private void initToptab() {
??//創建TopLayout
??????? TabLayout tabLayout = (TabLayout) rootView.findViewById(R.id.top_tab);
??????? //創建ViewPager
??????? mViewPager = (ViewPager) rootView.findViewById(R.id.tab_viewpager);
??????? //創建PagerAdapter
??????? adapter = new TabViewPagerAdapter(getActivity());???????
??????? mViewPager.setAdapter(adapter);
??????? //關鍵的語句,將ViewPager與TabLayout關聯(Tab title在adapter中設置,Pager Title)
??????? tabLayout.setupWithViewPager(mViewPager);

??????? //使用自定義Tab
??????? int tabs = tabLayout.getTabCount();
??????? for(int i = 0; i < tabs; i++) {
??????????? TabLayout.Tab tab = tabLayout.getTabAt(i);
??????????? View view = LayoutInflater.from(getActivity()).inflate(R.layout.indicator_tab, null, false);
??????????? tab.setCustomView(view);

??????? }
???????
??? }

??? PagerAdapter與直接使用ViewPager相同,重寫getPagerTitle(int position) 方法,為Tab設置title
??? @Override
??? public CharSequence getPageTitle(int position) {
??????? return pagers[position].getName();
??? }

轉載于:https://my.oschina.net/smuswc/blog/599633

總結

以上是生活随笔為你收集整理的TabLayout 与 FragmentTabHost的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。