使用STAR原则阐述项目
STAR:Situation(情景)、Task(任务)、Action(行动)和Result(结果)
情景:音乐播放器UI模式切换
任务:需要使用代码将某张图片设置为RadioButton的下标
行动:
- 尝试直接使用setCompoundDrawables,但是图片不显示。查看代码注释需要先给Drawable设置图片大小。
for (int i = 0; i < mTabGroup.getChildCount(); i++) {
Drawable homeIcon = SkinManager.getInstance().getDrawable(this, R.drawable.radiobutton_bottom_background_selector);
if (homeIcon != null) {
homeIcon.setBounds(0, 0, homeIcon.getIntrinsicWidth(), homeIcon.getIntrinsicHeight());
}
RadioButton radioButton = (RadioButton) mTabGroup.getChildAt(i);
radioButton.setTextColor(ContextCompat.getColorStateList(this, SkinManager.getInstance().getResourceId(this, R.color.textcolor_selector)));
radioButton.setCompoundDrawablesRelative(null, null, null, homeIcon);
radioButton.setCompoundDrawablePadding(DensityUtil.dp2px(-50f));
}
- 后面尝试调用了setCompoundDrawablesWithIntrinsicBounds、setCompoundDrawablesRelative、setPadding等方法都不管用。后面在setBounds中奖getIntrinsicHeight改为getMinimumHeight后图片显示出来了。
for (int i = 0; i < mTabGroup.getChildCount(); i++) {
Drawable homeIcon = SkinManager.getInstance().getDrawable(this,R.drawable.radiobutton_bottom_background_selector);
if (homeIcon != null) {
homeIcon.setBounds(0, 0, homeIcon.getIntrinsicWidth(), homeIcon.getMinimumHeight());
}
RadioButton radioButton = (RadioButton) mTabGroup.getChildAt(i);
radioButton.setTextColor(ContextCompat.getColorStateList(this, SkinManager.getInstance().getResourceId(this, R.color.textcolor_selector)));
radioButton.setCompoundDrawablesRelative(null, null, null, homeIcon);
radioButton.setCompoundDrawablePadding(DensityUtil.dp2px(-50f));
}
结果:播放器可以根据主题来切换下标
参考文章:
https://www.jianshu.com/p/6843f332c8df
https://vimsky.com/examples/detail/java-method-android.widget.TextView.setCompoundDrawablesRelative.html
2470

被折叠的 条评论
为什么被折叠?



