0

これは、私の PagerTitleStrip の XML です。

    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="500dp"
        android:scaleType="centerCrop">

        <android.support.v4.view.PagerTitleStrip
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/viepagertitlestrip"
            android:layout_gravity="top" />

    </android.support.v4.view.ViewPager>

以下は、私のカスタム PagerAdapter クラスです。

public class CustomPagerAdapter extends PagerAdapter {

    private int[] image_resources = {
            R.drawable.1,
            R.drawable.2,
            R.drawable.3,
    };
    private Context ctx;
    private LayoutInflater layoutInflater;
    public CustomPagerAdapter(Context ctx) {
        this.ctx = ctx;
    }

    @Override
    public int getCount() {
        return image_resources.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object o) {
        return (view == (RelativeLayout) o);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        String[] titlesArray = {
                "Title 1",
                "Title 2",
                "Title 3",
        };

        return titlesArray[position];
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View item_view = layoutInflater.inflate(R.layout.pager_item, container, false);
        ImageView imageview = (ImageView) item_view.findViewById(R.id.image_view);
        imageview.setImageResource(image_resources[position]);
        container.addView(item_view);
        return item_view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((RelativeLayout) object);
    }
}

PagerTitleStrip の XML 要素を変更するためのカスタム スタイル:

<style name="viepagertitlestrip">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:background">#d3d3d3</item>
    <item name="android:textColor">#666666</item>
    <item name="android:textSize">12sp</item>
</style>

個人的には、デフォルトで PagerTitleStrip が非常に醜いと思います。次のページのタイトルが画面の横に押しつぶされるのが嫌いです。私の画像配列には約20枚の画像があります。私は基本的に、各ページャーのタイトルを小さなドットにしたいと考えています。また、それらすべてを 1 つのページに均等に配置したいと考えています。これは可能ですか?

4

1 に答える 1

2

GitHubプロジェクトをビルドに取り込むには:

  1. JitPackリポジトリをビルド ファイルに追加します。

リポジトリの最後にある build.gradle に追加します。

repositories {
    // ...
    maven { url "https://jitpack.io" }
 }
  1. 依存関係を追加する

    dependencies {
    compile 'com.github.JakeWharton:ViewPagerIndicator:2.4.1@aar'
    }
    
于 2015-11-23T10:20:27.280 に答える