customize padding and margin in IconPageIndicator

人盡茶涼 提交于 2019-12-05 02:58:56

You are not setting margins correctly in right method.

Go Library project-->IconPageIndicator and edit notifyDataSetChanged() method as below:

  public void notifyDataSetChanged() {
    mIconsLayout.removeAllViews();
    IconPagerAdapter iconAdapter = (IconPagerAdapter) mViewPager.getAdapter();
    int count = iconAdapter.getCount();
    for (int i = 0; i < count; i++) {
        ImageView view = new ImageView(getContext(), null, R.attr.vpiIconPageIndicatorStyle);

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.setMargins(10 , 10, 0 , 0 ); // Here you can set margins.
        view.setLayoutParams(lp);

        view.setImageResource(iconAdapter.getIconResId(i));
        view.setTag(""+i);
        view.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                int viewPosition = Integer.parseInt(v.getTag().toString());

                mViewPager.setCurrentItem(viewPosition);
            }
        });
        mIconsLayout.addView(view);
    }
    if (mSelectedIndex > count) {
        mSelectedIndex = count - 1;
    }
    setCurrentItem(mSelectedIndex);
    requestLayout();
}

don't forget to clean the library project and rebuild it.I hope it works.

You can define the padding on the drawable itself like this:

indicator.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:left="4dp"
        android:right="4dp">
        <shape android:shape="oval">
            <solid android:color="@color/white" />

            <size
                android:width="10dp"
                android:height="10dp" />
        </shape>
    </item>
</layer-list>

and change your adapter to specify the icon resource like this:

private class MyViewPagerAdapter extends FragmentPagerAdapter implements IconPagerAdapter {
    // suppressed other adapter methods
    @Override
    public int getIconResId(int i) {
        return R.drawable.page_indicator;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!