章节目录
效果图
实战
TextView在xml的代码
<TextView
android:id="@+id/valueInfoTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="16sp"
android:text=" 2-18 18:13:16"
android:layout_marginTop="3dp"
android:layout_margin="3dp"
android:gravity="center"
android:drawableLeft="@mipmap/yijingqiandaoicon"
/>
java代码片段
if(Utils.getCustomRandomNumber(2)>=1){
holder.hisItemData.setText(" 已签到 " +timeString.substring(timeString.indexOf("-")+1));
holder.hisItemData.setTextColor(ResourceValuseUtils.getColor(context,R.color.blue_navy_06));
Drawable drawable = ResourceValuseUtils.getDrawable(context,R.mipmap.yijingqiandaoicon);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
holder.hisItemData.setCompoundDrawables(drawable,null,null,null);
holder.hisStateTv.setBackground(ResourceValuseUtils.getDrawable(context,R.drawable.his_shape_use_state));
holder.hisStateTv.setText("已使用");
}else {
holder.hisItemData.setText("未签到");
holder.hisItemData.setTextColor(ResourceValuseUtils.getColor(context,R.color.black));
holder.hisItemData.setCompoundDrawables(null,null,null,null);
holder.hisStateTv.setBackground(ResourceValuseUtils.getDrawable(context,R.drawable.his_shape_obsolete_state));
holder.hisStateTv.setText("已过期");
}
如果没显示图标,检查是否加入下面这句代码:
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
Drawable的setBounds
setBounds方法有四个参数:setBounds(int left, int top, int right, int bottom),这个四参数指的是drawable将在被绘制在canvas的哪个矩形区域内。
@Override
protected void onDraw(Canvas canvas) {
drawable.setBounds(100, 100, 500, 500);
drawable.draw(canvas);
}