您的位置:首页 > 移动开发 > Android开发

Android ViewPager中动态生成底部圆点

2016-06-24 11:23 465 查看
private void initCircleViews() {
LinearLayout ll_circle = (LinearLayout) findViewById(R.id.ll_circles);
circles = new ArrayList<>();

//        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.shape_circle_gray);
for (int i = 0; i < adapter.getPageCount(sl.getPerPageNum()); i++) {
View view = new View(this);
//            view.setLayoutParams(new ViewGroup.LayoutParams(bitmap.getWidth(),bitmap.getHeight()));
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(20, 20);
layoutParams.setMargins(10,0,0,0);
view.setLayoutParams(layoutParams);
view.setBackgroundResource(R.drawable.shape_circle_gray);
ll_circle.addView(view);
circles.add(view);
}
}
void setCircleView(int currentPage){
for (View view:circles){
view.setBackgroundResource(R.drawable.shape_circle_gray);
}
circles.get(currentPage).setBackgroundResource(R.drawable.shape_circle_red);
}

shape_circle_red.xml
shape_circle_gray.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >

<!-- 填充颜色 -->
<solid android:color="@android:color/darker_gray" ></solid>

<!--线的宽度,颜色灰色-->
<stroke android:width="2dp" android:color="@android:color/darker_gray"></stroke>

<!-- 矩形的圆角半径 -->
<corners android:radius="5dp" />

</shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >

<!-- 填充颜色 -->
<solid android:color="@android:color/holo_red_light" ></solid>

<!--线的宽度,颜色灰色-->
<stroke android:width="2dp" android:color="@android:color/holo_red_light"></stroke>

<!-- 矩形的圆角半径 -->
<corners android:radius="5dp" />

</shape>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: