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>
Android ViewPager中动态生成底部圆点
最新推荐文章于 2018-04-30 14:58:19 发布