自定义RadioButton小圆点,viewpager滑动改变颜色

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/feifei20170707/article/details/77881504
    //添加小圆点,根据你数据data的大小来定义个数
    for (int i = 0; i < data.size(); i++) {
        View view = new View(this);
        //重新设置view的大小
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
        layoutParams.width = 15;
        layoutParams.height = 15;
        //设置间距
        layoutParams.rightMargin = 10;
        //把这个LayoutParams设置给view
        view.setLayoutParams(layoutParams);
        //设置第一个为选中状态
        if (i == 0) {
            view.setBackgroundResource(R.drawable.view1);
        } else
            view.setBackgroundResource(R.drawable.view2);
        //把view添加到LinearLayout
        layout.addView(view);
    }
    //viewpager滑动改变小圆点状态
    home_viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            for (int i = 0; i < data.size(); i++) {
                View view = layout.getChildAt(i);
                if (i == position)
                    view.setBackgroundResource(R.drawable.view1);
                else
                    view.setBackgroundResource(R.drawable.view2);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });
}
下面是在drawable定义的shape
view1 定义的圆,样式自定义,也可以定义小方块,小长条等
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="@color/colorAccent"/>

</shape>
view2定义
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="@color/colorPrimary" />

</shape>


          
展开阅读全文

没有更多推荐了,返回首页