Android-ViewPagerIndicator框架使用——UnderlinePageIndicator


前言:UnderlinePageIndicator這個指示,是一個很小巧的東西,簡單,沒有那么多復雜的效果。

    一:布局定義simple_underlines:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />
    <com.viewpagerindicator.UnderlinePageIndicator
        android:id="@+id/indicator"
        android:layout_height="2dp"
        android:layout_width="fill_parent"
        />
</LinearLayout>

    二:代碼中調用:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.simple_underlines);

        mAdapter = new TestFragmentAdapter(getSupportFragmentManager());

        mPager = (ViewPager)findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);

        mIndicator = (UnderlinePageIndicator)findViewById(R.id.indicator);
        mIndicator.setViewPager(mPager);
    }

    三:可改變的自定義屬性:

    <declare-styleable name="UnderlinePageIndicator">

        <!-- 指示是否隱藏 -->
        <attr name="fades" format="boolean" />
        <!-- 延遲多久之后隱藏 -->
        <attr name="fadeDelay" format="integer" />
        <!-- 變到全透明的時間 -->
        <attr name="fadeLength" format="integer" />
        <!-- 指示條的顏色 -->
        <attr name="selectedColor" />
        <!-- 控件的背景色 -->
        <attr name="android:background" />
    </declare-styleable>

    四:改變屬性的三種方法:

      1.布局中改變:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <com.viewpagerindicator.UnderlinePageIndicator
        android:id="@+id/indicator"
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:background="#FFCCCCCC"
        app:fadeDelay="1000"
        app:fadeLength="1000"
        app:selectedColor="#FFCC0000" />

</LinearLayout>

      3.代碼中改變:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.simple_underlines);

        mAdapter = new TestFragmentAdapter(getSupportFragmentManager());

        mPager = (ViewPager)findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);

        UnderlinePageIndicator indicator = (UnderlinePageIndicator)findViewById(R.id.indicator);
        mIndicator = indicator;
        indicator.setViewPager(mPager);
        indicator.setSelectedColor(0xFFCC0000);
        indicator.setBackgroundColor(0xFFCCCCCC);
        indicator.setFadeDelay(1000);
        indicator.setFadeLength(1000);
    }

      3.theme主題定義:

    <style name="StyledIndicators" parent="@android:style/Theme.Light">
        <item name="vpiUnderlinePageIndicatorStyle">@style/CustomUnderlinePageIndicator</item>
    </style>

    <style name="CustomUnderlinePageIndicator">
        <item name="selectedColor">#FFCC0000</item>
        <item name="android:background">#FFCCCCCC</item>
        <item name="fadeLength">1000</item>
        <item name="fadeDelay">1000</item>
    </style>

      使用主題:

        <activity
            android:name=".SampleUnderlinesStyledTheme"
            android:label="Underlines/Styled (via theme)"
            android:theme="@style/StyledIndicators" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="com.jakewharton.android.viewpagerindicator.sample.SAMPLE" />
            </intent-filter>
        </activity>

源碼以及Demo下載地址:http://download.csdn.net/detail/as294985925/6796117


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM