Android-ViewPagerIndicator框架使用——LinePageIndicator


前言:LinePageIndicator類似CirclePageIndicator,只是將圓點指示變成了長條指示。

    一:使用是定義的布局文件simple_lines  :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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.LinePageIndicator
        android:id="@+id/indicator"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dip" />
</LinearLayout>

    二:代碼中使用:

        setContentView(R.layout.simple_lines);

        mAdapter = new TestFragmentAdapter(getSupportFragmentManager());

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

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

    三:修改屬該改變樣式,下邊可以修改的屬性值:

<declare-styleable name="LinePageIndicator">

        <!-- 是否居中顯示 -->
        <attr name="centered" />
        <!-- 沒有被選擇標識的顏色 -->
        <attr name="unselectedColor" />
        <!-- 被選擇標識的顏色 -->
        <attr name="selectedColor" />
        <!-- 標識的寬度大小 -->
        <attr name="lineWidth" format="dimension" />
        <!-- 標識的高度 -->
        <attr name="strokeWidth" />
        <!-- 標識間隔大小 -->
        <attr name="gapWidth" format="dimension" />
        <!-- 整體的背景色 -->
        <attr name="android:background" />
    </declare-styleable>

      1.代碼修改樣式:

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

        mAdapter = new TestFragmentAdapter(getSupportFragmentManager());

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

        LinePageIndicator indicator = (LinePageIndicator) findViewById(R.id.indicator);
        mIndicator = indicator;
        indicator.setViewPager(mPager);
        // 獲得屏幕的密度
        final float density = getResources().getDisplayMetrics().density;
        indicator.setSelectedColor(0x88FF0000);
        indicator.setUnselectedColor(0xFF888888);
        indicator.setStrokeWidth(4 * density);
        indicator.setLineWidth(30 * density);
    }

      2.布局中修改:

<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.LinePageIndicator
        android:id="@+id/indicator"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dip"
        app:lineWidth="30dp"
        app:selectedColor="#FF880000"
        app:strokeWidth="4dp"
        app:unselectedColor="#FF888888" />
</LinearLayout>

      3.主題方式修改:

        <activity
            android:name=".SampleLinesStyledTheme"
            android:label="Lines/Styled (via theme)" android:theme="@style/CustomLinePageIndicator" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

      主題:

    <style name="CustomLinePageIndicator" parent="@android:style/Theme.Light">
        <item name="strokeWidth">4dp</item>
        <item name="lineWidth">30dp</item>
        <item name="unselectedColor">#FF888888</item>
        <item name="selectedColor">#FF880000</item>
    </style>

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


免責聲明!

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



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