您好,登錄后才能下訂單哦!
效果圖如下
首先,要使用控件需要添加design library,在Android Studio中添加
compile 'com.android.support:design:23.4.0'
然后是布局文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" tools:context="com.lg.tablayoutdemo.MainActivity"> <android.support.design.widget.TabLayout android:id="@+id/tab_layou" android:layout_width="match_parent" app:tabIndicatorColor="@android:color/holo_blue_light" app:tabTextColor="@android:color/darker_gray" app:tabSelectedTextColor="@android:color/black" android:layout_height="wrap_content" /> <android.support.v4.view.ViewPager android:id="@+id/view_pager" android:layout_below="@id/tab_layou" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
其中TabLayout中tabIndicatorColor屬性為標(biāo)簽底部下滑線(xiàn)顏色,tabTextColor為標(biāo)簽未選中時(shí)字體顏色,tabSelectedTextColor為選中時(shí)字體顏色
自定一個(gè)FragmentPagerAdapter適配器
public class MyViewPagerAdapter extends FragmentPagerAdapter { private List<Fragment> fragments; private String[] titles; public MyViewPagerAdapter(FragmentManager fm, String[] titles, List<Fragment> fragments) { super(fm); this.titles = titles; this.fragments = fragments; } @Override public Fragment getItem(int arg0) { return fragments.get(arg0); } @Override public CharSequence getPageTitle(int position) { return titles[position]; } @Override public int getCount() { return fragments.size(); } }
Fragment代碼我就不貼了,會(huì)在下面奉上源碼地址
public class MainActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener { private TabLayout tabLayout; private ViewPager viewPager; private MyViewPagerAdapter viewPagerAdapter; //TabLayout標(biāo)簽 private String[] titles=new String[]{"ANDROID","JAVA","C#","PHP"}; private List<Fragment> fragments=new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init(){ tabLayout=(TabLayout)findViewById(R.id.tab_layou); viewPager=(ViewPager)findViewById(R.id.view_pager); //設(shè)置TabLayout標(biāo)簽的顯示方式 tabLayout.setTabMode(TabLayout.MODE_FIXED); //循環(huán)注入標(biāo)簽 for (String tab:titles){ tabLayout.addTab(tabLayout.newTab().setText(tab)); } //設(shè)置TabLayout點(diǎn)擊事件 tabLayout.setOnTabSelectedListener(this); fragments.add(new AndroidFragment()); fragments.add(new JavaFragment()); fragments.add(new CshapFragment()); fragments.add(new PhpFragment()); viewPagerAdapter=new MyViewPagerAdapter(getSupportFragmentManager(),titles,fragments); viewPager.setAdapter(viewPagerAdapter); tabLayout.setupWithViewPager(viewPager); } @Override public void onTabSelected(TabLayout.Tab tab) { viewPager.setCurrentItem(tab.getPosition()); } @Override public void onTabUnselected(TabLayout.Tab tab) { } @Override public void onTabReselected(TabLayout.Tab tab) { } }
以上是核心代碼,至此功能就實(shí)現(xiàn)了,當(dāng)然也可 以根據(jù)不同的需求進(jìn)行改動(dòng)
源碼地址:http://down.51cto.com/data/2221954
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。