Android仿网易新闻导航栏PagerSlidingTabStrip
效果圖:
Github:https://github.com/astuetz/PagerSlidingTabStrip
使用方法
1、下載Zip壓縮文件
從?PagerSlidingTabStrip官網(wǎng)(https://github.com/astuetz/PagerSlidingTabStrip)上下載Zip文件,下載成功后解壓縮Zip文件,如下:
2、導(dǎo)入library和sample項目
Import -> Android -> Existing Android Code Into Workspace -> Browse,選擇你解壓縮的library 目錄即可,記住勾選?Copy projects into workset?
3、具體用法
導(dǎo)入后的項目結(jié)構(gòu)如下:
我簡單翻譯一下官網(wǎng)上的內(nèi)容:
交互式頁面指示器控件,完美配合ViewPager控件(來自Android Support Library)
注意:這里的ViewPager的適配器必須是繼承的FragmentPagerAdapter,并重寫getPageIconResId(int position)或者getPageTitle(int position)方法
PagerSlidingTabStrip需要配合ViewPager一起使用,PagerSlidingTabStrip的作用相當(dāng)于一個頂部的導(dǎo)航條
MainActivity 布局如下:
<pre name="code" class="html"><RelativeLayout 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" ><com.astuetz.PagerSlidingTabStripandroid:id="@+id/tabs"android:layout_width="match_parent"android:layout_height="48dip"android:background="@drawable/background_tabs" /><android.support.v4.view.ViewPagerandroid:id="@+id/pager"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@+id/tabs"tools:context=".MainActivity" /></RelativeLayout>
MainActivity的onCreate方法如下:
在onCreate方法(或者onCreateView對于一個fragment),綁定PagerSlidingTabStrip控件到ViewPager上
<pre name="code" class="java">
.獲取PagerSlidingTabStrip控件,綁定ViewPager
fragment,都是很簡單的設(shè)置了不同的顏色背景而已,我這里就不再貼出代碼了
[java]?view plaincopyprint?
ViewPager Adapter?代碼如下:
其中,1.android:layout_below="@id/tabs"一定要記得寫上,是讓ViewPager控件在tabs之下。
2. ??xmlns:app="http://schemas.android.com/apk/res/com.example.viewfragment, 這句話是我的理解是,如果你想要用PagerSlidingTabStrip,就必須加上,不過他會自動加,不需要你來管他。
<pre name="code" class="java">public class MyPagerAdapter extends FragmentPagerAdapter {private final String[] TITLES = { "Categories", "Home", "Top Paid", "Top Free", "Top Grossing", "Top New Paid","Top New Free", "Trending" };public MyPagerAdapter(FragmentManager fm) {super(fm);}@Overridepublic CharSequence getPageTitle(int position) {return TITLES[position];}@Overridepublic int getCount() {return TITLES.length;}@Overridepublic Fragment getItem(int position) {return SuperAwesomeCardFragment.newInstance(position);}} 這樣就已經(jīng)完成了,很簡單吧~!
個性化設(shè)置
為了讓你的app不像另一個?Play Store上面的app,你可以添加這些屬性來做出自己獨具一格的應(yīng)用。
- pstsIndicatorColor?Color of the sliding indicator ?滑動條的顏色
- pstsUnderlineColor?Color of the full-width line on the bottom of the view ?滑動條所在的那個全寬線的顏色
- pstsDividerColor?Color of the dividers between tabs ? 每個標(biāo)簽的分割線的顏色
- pstsIndicatorHeightHeight of the sliding indicator ? ? ? 滑動條的高度
- pstsUnderlineHeight?Height of the full-width line on the bottom of the view ? ?滑動條所在的那個全寬線的高度
- pstsDividerPadding?Top and bottom padding of the dividers ? 分割線底部和頂部的填充寬度
- pstsTabPaddingLeftRight?Left and right padding of each tab ? 每個標(biāo)簽左右填充寬度
- pstsScrollOffset?Scroll offset of the selected tab
- pstsTabBackground?Background drawable of each tab, should be a StateListDrawable ?每個標(biāo)簽的背景,應(yīng)該是一個StateListDrawable ?
- pstsShouldExpand?If set to true, each tab is given the same weight, default false ? 如果設(shè)置為true,每個標(biāo)簽是相同的控件,均勻平分整個屏幕,默認(rèn)是false
- pstsTextAllCaps?If true, all tab titles will be upper case, default true ? 如果為true,所有標(biāo)簽都是大寫字母,默認(rèn)為true
所有的屬性都有他們自己的getter和setter方法來隨時改變他們
關(guān)于TypedValue.applyDimension()這個方法,你可以查看 http://blog.csdn.net/harryweasley/article/details/42172321
關(guān)于params.setMargins(margin, margin, margin, margin);你可以查看http://www.itnose.net/detail/6038494.html
這里就不在解釋了
1.關(guān)于cannot convert from Fragment1 to Fragment這個錯誤,你可以查看http://blog.csdn.net/jason0539/article/details/9712273
2.關(guān)于TypedValue.applyDimension()這個方法,你可以查看http://blog.csdn.net/harryweasley/article/details/42172321
3.關(guān)于params.setMargins(margin, margin, margin, margin);你可以查看http://www.itnose.net/detail/6038494.html
總結(jié)
以上是生活随笔為你收集整理的Android仿网易新闻导航栏PagerSlidingTabStrip的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android 编程规范与常用技巧
- 下一篇: 利用swipelistview完成qq聊