android 標題下劃線分塊導航 使用ViewPager+ViewPagerIndicator

看了好多人的博客,然後自己總結一下。
先上圖



0. 在libs添加
android-support-v4.jar

1. 引用工程
把github上的ViewPagerIndicator項目引用到項目工程

2. xml的使用
TabPageIndicator 跟 ViewPager沒什麼關係,各自獨立

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.viewpagerindicator.TabPageIndicator
        android:id="@+id/tpi_titles"
        android:layout_width="fill_parent"
        android:layout_height="45dp" />

    <android.support.v4.view.ViewPager
        android:id="@+id/vp_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>



3. 代碼引用
TabPageIndicator mTpiTitles;
ViewPager mVpMain;

List<String> titles = new ArrayList<String>(); //標題
List<View> pageViewList;

public void initViewPager() {
    // 初始化title
    titles.add("詳情");
    titles.add("評論");

    // 初始化view
    View view = LayoutInflater.from(this).inflate(R.layout.food_detail, null);

    ImageView view2 = new ImageView(this);
<pre name="code" class="java" style="font-size: 15px; orphans: 2; widows: 2;">    v<span style="font-family: Tahoma; text-align: -webkit-auto;">iew2.setBackgroundColor(Color.CYAN);</span>
pageViewList = new ArrayList<View>(); pageViewList.add(view); pageViewList.add(view2); PagerAdapter mPagerAdapter = new PagerAdapter() { @Override public int getCount() { // TODO Auto-generated method stub return pageViewList.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0 == arg1; } @Override public void destroyItem(View arg0, int arg1, Object arg2) { ((ViewPager) arg0).removeView(pageViewList.get(arg1)); } @Override public Object instantiateItem(View arg0, int arg1) { ((ViewPager) arg0).addView(pageViewList.get(arg1)); return pageViewList.get(arg1); } @Override public CharSequence getPageTitle(int position) { return titles.get(position); } }; // 設置adapter mVpMain.setAdapter(mPagerAdapter); // 把TabPageIndicator跟ViewPager關聯 mTpiTitles.setViewPager(mVpMain);}




4. 定義一個顏色selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:color="@color/orange" />
    <item android:state_focused="true" android:color="@color/orange" />
    <item android:color="@color/text_color" />
</selector>   



5. 在style.xml里加入代碼

這裏定義了indicator的風格

    
<style name="MyTabPageIndicator" parent="AppBaseTheme">  <!-- 這裏要跟整個app的風格一致,比如NoTitle  -->
    <item name="vpiTabPageIndicatorStyle">@style/MyWidget.TabPageIndicator</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:animationDuration">5000</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

<style name="MyWidget.TabPageIndicator" parent="Widget.TabPageIndicator">
    <item name="android:gravity">center</item>    <pre name="code" class="html" style="font-size: 15px; orphans: 2; widows: 2;">    <!-- 資源引用在ViewPagerIndicator裏,改變indicator背景  -->
<item name="android:background">@drawable/vpi__tab_indicator</item> <item name="android:paddingLeft">5dip</item> <item name="android:paddingRight">5dip</item> <item name="android:paddingTop">8dp</item> <item name="android:paddingBottom">8dp</item> <item name="android:textStyle">bold</item> <item name="android:textColor">@drawable/selecter_tab</item> <item name="android:textSize">14sp</item> <item name="android:maxLines">1</item></style>


6. AndroidManifest.xml添加代碼  
android:theme="@style/MyTabPageIndicator"
如:
<activity android:name=".ui.activity.TestActivity" 
    android:theme="@style/MyTabPageIndicator" />





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章