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

原文地址:https://www.cnblogs.com/qinghuaideren/p/3502214.html