Android使用TabLayout+Fragment實(shí)現(xiàn)頂部選項(xià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的寫法非常簡單,在自定義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ì)大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
解析Android開發(fā)優(yōu)化之:對(duì)界面UI的優(yōu)化詳解(二)
在一個(gè)應(yīng)用程序中,一般都會(huì)存在多個(gè)Activity,每個(gè)Activity對(duì)應(yīng)著一個(gè)UI布局文件。一般來說,為了保持不同窗口之間的風(fēng)格統(tǒng)一,在這些UI布局文件中,幾乎肯定會(huì)用到很多相同的布局2013-05-05
關(guān)于ADB的Android Debug Bridge(安卓調(diào)試橋)那些事
這篇文章主要介紹了關(guān)于ADB的Android Debug Bridge(安卓調(diào)試橋)那些事,需要的朋友可以參考下2019-10-10
Android EditTextView 實(shí)現(xiàn)帶空格分隔的輸入(電話號(hào)碼,銀行卡)
這篇文章主要介紹了Android EditTextView 實(shí)現(xiàn)帶空格分隔的輸入(電話號(hào)碼,銀行卡)的相關(guān)資料,需要的朋友可以參考下2018-02-02
Android實(shí)現(xiàn)橫屏切換科學(xué)計(jì)算器
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)橫屏切換科學(xué)計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06
Android 實(shí)現(xiàn)定時(shí)器的四種方式總結(jié)及實(shí)現(xiàn)實(shí)例
這篇文章主要介紹了Android 實(shí)現(xiàn)定時(shí)器的四種方式總結(jié)及實(shí)現(xiàn)實(shí)例的相關(guān)資料,這里對(duì)定時(shí)器進(jìn)行詳解,并附實(shí)例代碼,需要的朋友可以參考下2016-12-12
Android編程實(shí)現(xiàn)仿iphone抖動(dòng)效果的方法(附源碼)
這篇文章主要介紹了Android編程實(shí)現(xiàn)仿iphone抖動(dòng)效果的方法,結(jié)合實(shí)例形式分析了仿iphone抖動(dòng)效果的頁面布局及功能實(shí)現(xiàn)技巧,并附帶實(shí)例源碼供讀者下載,需要的朋友可以參考下2015-11-11

