首先扯點別的:最近感覺自己腐敗了,學習也沒勁了,鍛煉也不積極了,整天吹牛扯淡,意志漸漸消磨,理想慢慢模糊,眼看就要墮入混吃等死的地步了。但是,經過痛苦的掙扎,激烈的內心斗爭,我還是決定依然往前走,去迎接生命中的挑戰與逆境,歡樂與驚喜。“棄我去者,昨日之日不可留。”今天就應該戰斗,努力堅持,為了自己的理想,為了自己的責任。 ps:今天有兩件事很高興。1:跟一個哥們(苗哥)喝了一次酒,失去的csdn博客找回來了。然后是今天看到自己博客沒有gif效果,有點挫,所以改一下展示的效果。
效果1

效果2:實現起來有點麻煩,適用于fragment個數固定而且標題圖標和文字不變的情況。 
第一 這是Design Support Library中的控件,在build.gradle中添加
compile 'com.android.support:design:23.2.0'12 12
12接下來實現第一個效果,比較簡單。
第二:activity_tab_layout.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"><!--TabLayout控件> <android.support.design.widget.TabLayout android:id="@+id/tabLayout" android:layout_width="match_parent" android:layout_height="wrap_content" /> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /></LinearLayout>1234567891011121314151617 1234567891011121314151617
1234567891011121314151617第三TabLayoutActivity.java文件
public class TabLayoutActivity extends AppCompatActivity { @BindView(R.id.tabLayout) TabLayout tabLayout; @BindView(R.id.viewPager) ViewPager viewPager; PRivate List<Fragment> fragments; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_tab_layout); ButterKnife.bind(this); //設置tabLayout的屬性 tabLayout = (TabLayout) findViewById(R.id.tabLayout); tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); tabLayout.setTabMode(TabLayout.MODE_FIXED); tabLayout.setBackgroundColor(Color.parseColor("#ffffff")); //設置tab上文字的顏色,第一個參數表示沒有選中狀態下的文字顏色,第二個參數表示選中后的文字顏色 tabLayout.setTabTextColors(Color.parseColor("#000000"), Color.parseColor("#0ddcff")); //設置tab選中的底部的指示條的顏色 tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#0ddcff")); fragments = new ArrayList<>(); //給fragments 添加三個fragment fragments.add(BlankFragment.newInstance("百度fragment")); fragments.add(BlankFragment.newInstance("騰訊fragment")); fragments.add(BlankFragment.newInstance("阿里fragment")); //給viewPager設置適配器 viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int position) { return fragments.get(position); } @Override public int getCount() { return fragments.size(); } @Override public CharSequence getPageTitle(int position) { switch (position) { case 0: return "百度"; case 1: return "騰訊"; case 2: return " 阿里"; } return "沒有標題"; } }); //然后讓TabLayout和ViewPager關聯,只需要一句話,簡直也是沒誰了. tabLayout.setupWithViewPager(viewPager); }}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859BlankFragment.java
public class BlankFragment extends Fragment { private static final String ARG_PARAM1 = "param1"; @BindView(R.id.text_fragment) TextView textFragment; private String mParam1; public BlankFragment() { // Required empty public constructor } public static BlankFragment newInstance(String param1) { BlankFragment fragment = new BlankFragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_blank2, container, false); ButterKnife.bind(this, view); textFragment.setText(mParam1); return view; }}1234567891011121314151617181920212223242526272829303132333435363738 1234567891011121314151617181920212223242526272829303132333435363738
1234567891011121314151617181920212223242526272829303132333435363738fragment_blank2.xml
<FrameLayout 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" tools:context="com.humanheima.viewpagerdemo.ui.fragment.BlankFragment"> <TextView android:id="@+id/text_fragment" android:layout_width="match_parent" android:layout_height="match_parent" /></FrameLayout>12345678910111213 12345678910111213
12345678910111213實現第二種效果
activity_tab_layout.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.design.widget.TabLayout android:id="@+id/tabLayout" android:layout_width="match_parent" android:layout_height="64dp" app:tabBackground="@color/colorPrimary" app:tabGravity="fill" app:tabIndicatorColor="@color/colorAccent" app:tabIndicatorHeight="2dp"> </android.support.design.widget.TabLayout> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /></LinearLayout>1234567891011121314151617181920212223 1234567891011121314151617181920212223
1234567891011121314151617181920212223TabLayoutActivity .java文件
public class TabLayoutActivity extends AppCompatActivity { @BindView(R.id.tabLayout) TabLayout tabLayout; @BindView(R.id.viewPager) ViewPager viewPager; private List<Fragment> fragments; private TabLayout.Tab tabQQ, tabSina, tabWechat; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_tab_layout); ButterKnife.bind(this); //tab,添加setCustomView tabQQ = tabLayout.newTab().setCustomView(R.layout.tab_item_qq); tabSina = tabLayout.newTab().setCustomView(R.layout.tab_item_sina); tabWechat = tabLayout.newTab().setCustomView(R.layout.tab_item_weixin); //tabLayout addTab tabLayout.addTab(tabQQ); tabLayout.addTab(tabSina); tabLayout.addTab(tabWechat); fragments = new ArrayList<>(); //給fragments 添加三個fragment fragments.add(BlankFragment.newInstance("QQfragment")); fragments.add(BlankFragment.newInstance("微博fragment")); fragments.add(BlankFragment.newInstance("微信fragment")); //給viewPager設置適配器 viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int position) { return fragments.get(position); } @Override public int getCount() { return fragments.size(); } }); //tabLayout 添加tab切換的監聽事件 tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { //得到當前選中的tab的位置,切換相應的fragment int nowPosition = tab.getPosition(); viewPager.setCurrentItem(nowPosition); } @Override public void onTabUnselected(TabLayout.Tab tab) { } @Override public void onTabReselected(TabLayout.Tab tab) { } }); //給viewPager添加切換監聽, viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { //選中相應的tab switch (position) { case 0: tabQQ.select(); break; case 1: tabSina.select(); break; case 2: tabWechat.select(); break; default: break; } } @Override public void onPageScrollStateChanged(int state) { } }); //然后讓TabLayout和ViewPager關聯這句話不需要了。 //tabLayout.setupWithViewPager(viewPager); }}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596customView的布局文件,只貼一個,R.layout.tab_item_weixin。
<?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:orientation="vertical"> <ImageView android:id="@+id/img_weixin" android:layout_width="32dp" android:layout_height="32dp" android:src="@m1234567891011121314151617181920 1234567891011121314151617181920
1234567891011121314151617181920參考網址 【1】http://blog.csdn.net/aigestudio/article/details/47155769 【2】http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0731/3247.html 【3】https://developer.android.com/reference/android/support/design/widget/TabLayout.html#attr_android.support.design:tabBackground 【4】https://material.google.com/components/tabs.html#tabs-specs 結尾:Android 新特性自己了解的很少,自己要多多關注,堅持學下去!也希望和我一樣的android小白,不放棄,不拋棄,堅持學習,終會有收獲!
(function () {('pre.prettyprint code').each(function () { var lines =新聞熱點
疑難解答