AndroidAndroid_孤独患者的旅行Android技术知识

Android 5.x Material design 之 Ta

2015-12-30  本文已影响918人  kmzhao

Alex_孤独患者

0__ 说在前边的话

01_ TabLayout的概述 和 效果 图片

4078BCBB-C202-4234-8CF2-C3914D725255.png

02_如何玩这个呢

dependencies { 
compile   "com.android.support:appcompatv7:${supportLibVersion}" 
compile "com.android.support:design:${supportLibVersion}"
}

注意: ${supportLibVersion} 是根据你自己拥有的版本来填写
例如: 我的是

  compile 'com.android.support:appcompat-v7:23.1.1'
  compile 'com.android.support:design:23.1.1'
<?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/tl_sliding"        
          android:layout_width="match_parent"        
          android:layout_height="wrap_content" />    
     <android.support.v4.view.ViewPager        
          android:id="@+id/vp_content"        
          android:layout_width="match_parent"        
          android:layout_height="0dp"        
          android:layout_weight="1">
    </android.support.v4.view.ViewPager>
</LinearLayout>
      public class CommonFragment extends Fragment {
            private static final String CF_PAGE = "cf_page";
            private int mPage;
            //初始化Fragment
            public static CommonFragment newInstance(int page) {
                          Bundle args = new Bundle();
                          args.putInt(CF_PAGE, page);
                          CommonFragment fragment = new CommonFragment();
                          fragment.setArguments(args);
                          return fragment;
            }
          @Override
            public void onCreate(@Nullable Bundle savedInstanceState) {  
                  super.onCreate(savedInstanceState);   
                  mPage = getArguments().getInt(CF_PAGE);
            }
//
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {    
                      TextView tv = new TextView(getActivity());                                            
                      tv.setText("fragments---" + mPage);  
                      return tv;
             }

简书的代码,写的我好蛋疼啊。。。怎么格式化。。。累死宝宝了。。。

public class PagerAdaper extends FragmentPagerAdapter {  
          /** 标题栏 */ 
          private static final String[] mTitles = {"tab0","tab1", "tab2", "tab3", "tab4", "tab5", "tab6"};   
          public PagerAdaper(FragmentManager fm) {   
                 super(fm);    
          } 
           @Override   
           public Fragment getItem(int position) {  
                return CommonFragment.newInstance(position); 
           }    
          @Override    
          public int getCount() {    
              return mTitles.length;
          }   
          //设置标题
         @Override 
         public CharSequence getPageTitle(int position) {
                  return mTitles[position];
          }}

已经无力吐槽~~ 这段代码很简单,主要的方法是getPageTitle(position) ,通过这个方法,可以给Tab设置标题

public class MainActivity extends FragmentActivity {   
       @Override    
       protected void onCreate(Bundle savedInstanceState) {              
                super.onCreate(savedInstanceState);        
                setContentView(R.layout.activity_main);       
                ViewPager vp_content = (ViewPager) findViewById(R.id.vp_content);
                TabLayout tl_sliding = (TabLayout) findViewById(R.id.tl_sliding); 
               vp_content.setAdapter(new PagerAdaper(getSupportFragmentManager()));        
              /**让标题栏和viewpager联动起来*/        
              tl_sliding.setupWithViewPager(vp_content);
        }}

**当然,如果给TabLayout设置方法,tl_sliding.setTabMode(TabLayout.MODE_SCROLLABLE);标题栏就可以自动滚动起来了,如图所示: **

10EDE03D-3F93-4543-B706-D11BD9C299B0.png

最后 ,当然TabLayout的进阶,明天再写咯。。。晚安咯。。。 可以看第二篇 文章~

Android 5.x Material design 之 TabLayout02

上一篇下一篇

猜你喜欢

热点阅读