以下是一段是布局代码
<cn.bingoogolapple.bgabanner.BGABanner
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="120dp"
app:banner_pageChangeDuration="1000"
app:banner_pointAutoPlayInterval="4000"
app:banner_indicatorGravity="bottom|center_horizontal"
app:banner_isNeedShowIndicatorOnOnlyOnePage="true"
app:banner_pointContainerBackground="@color/transparent"
app:banner_pointContainerLeftRightPadding="10dp"
app:banner_pointDrawable="@drawable/banner_selector"
app:banner_transitionEffect="cube"
/>
现在我这边需要通过切换皮肤主题 修改选中点的颜色 及banner_pointDrawable的值
通过源码发现 该Drawable的设置在BGABanner这个类中是通过私有属性mPointDrawableResId去接收的。属性声明:private int mPointDrawableResId
源码如下
private void initCustomAttr(int attr, TypedArray typedArray) {
if (attr == R.styleable.BGABanner_banner_pointDrawable) {
mPointDrawableResId = typedArray.getResourceId(attr, R.drawable.bga_banner_selector_point_solid);
} else if (attr == R.styleable.BGABanner_banner_pointContainerBackground) {
mPointContainerBackgroundDrawable = typedArray.getDrawable(attr);
} else if (attr == R.styleable.BGABanner_banner_pointLeftRightMargin) {
mPointLeftRightMargin = typedArray.getDimensionPixelSize(attr, mPointLeftRightMargin);
} else if (attr == R.styleable.BGABanner_banner_pointContainerLeftRightPadding) {
mPointContainerLeftRightPadding = typedArray.getDimensionPixelSize(attr, mPointContainerLeftRightPadding);
} else if (attr == R.styleable.BGABanner_banner_pointTopBottomMargin) {
mPointTopBottomMargin = typedArray.getDimensionPixelSize(attr, mPointTopBottomMargin);
} else if (attr == R.styleable.BGABanner_banner_indicatorGravity) {
mPointGravity = typedArray.getInt(attr, mPointGravity);
} else if (attr == R.styleable.BGABanner_banner_pointAutoPlayAble) {
mAutoPlayAble = typedArray.getBoolean(attr, mAutoPlayAble);
} else if (attr == R.styleable.BGABanner_banner_pointAutoPlayInterval) {
mAutoPlayInterval = typedArray.getInteger(attr, mAutoPlayInterval);
} else if (attr == R.styleable.BGABanner_banner_pageChangeDuration) {
mPageChangeDuration = typedArray.getInteger(attr, mPageChangeDuration);
} else if (attr == R.styleable.BGABanner_banner_transitionEffect) {
int ordinal = typedArray.getInt(attr, TransitionEffect.Accordion.ordinal());
mTransitionEffect = TransitionEffect.values()[ordinal];
} else if (attr == R.styleable.BGABanner_banner_tipTextColor) {
mTipTextColor = typedArray.getColor(attr, mTipTextColor);
} else if (attr == R.styleable.BGABanner_banner_tipTextSize) {
mTipTextSize = typedArray.getDimensionPixelSize(attr, mTipTextSize);
} else if (attr == R.styleable.BGABanner_banner_placeholderDrawable) {
mPlaceholderDrawableResId = typedArray.getResourceId(attr, mPlaceholderDrawableResId);
} else if (attr == R.styleable.BGABanner_banner_isNumberIndicator) {
mIsNumberIndicator = typedArray.getBoolean(attr, mIsNumberIndicator);
} else if (attr == R.styleable.BGABanner_banner_numberIndicatorTextColor) {
mNumberIndicatorTextColor = typedArray.getColor(attr, mNumberIndicatorTextColor);
} else if (attr == R.styleable.BGABanner_banner_numberIndicatorTextSize) {
mNumberIndicatorTextSize = typedArray.getDimensionPixelSize(attr, mNumberIndicatorTextSize);
} else if (attr == R.styleable.BGABanner_banner_numberIndicatorBackground) {
mNumberIndicatorBackground = typedArray.getDrawable(attr);
} else if (attr == R.styleable.BGABanner_banner_isNeedShowIndicatorOnOnlyOnePage) {
mIsNeedShowIndicatorOnOnlyOnePage = typedArray.getBoolean(attr, mIsNeedShowIndicatorOnOnlyOnePage);
} else if (attr == R.styleable.BGABanner_banner_contentBottomMargin) {
mContentBottomMargin = typedArray.getDimensionPixelSize(attr, mContentBottomMargin);
} else if (attr == R.styleable.BGABanner_android_scaleType) {
final int index = typedArray.getInt(attr, -1);
if (index >= 0 && index < sScaleTypeArray.length) {
mPlaceholderScaleType = sScaleTypeArray[index];
}
}
}
即第一个属性是我们需要去修改的
修改方案如下:
try {
Field field1 = banner.getClass().getDeclaredField("mPointDrawableResId");
field1.setAccessible(true);
field1.set(banner,ColorAPI.getResourceId(R.drawable.banner_selector));
} catch (Exception e) {
e.printStackTrace();
}
先获取mPointDrawableResId属性字段field1,然后设置可以访问(accessible为true),再设置我们修改修改成为的drawable,如此即可实现。