目前公司有一个商品图片动态增加标签的功能,标签为四张图片,根据不同的设置放在RelativeLayout四个角上
布局样式:
<RelativeLayout android:id="@+id/img_rel" android:layout_width="90dp" android:layout_height="90dp" android:layout_alignParentLeft="true" android:layout_marginLeft="10dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp"> <ImageView android:id="@+id/img" android:layout_width="90dp" android:layout_height="90dp" android:scaleType="fitXY" /> </RelativeLayout>
页面调用:
if (promoListVo.getTags()!=null&&promoListVo.getTags().size()>0){ ViewHelperUtils.addProductTags(90,90,promoListVo.getTags(),context,bitmapUtils,viewHolder.img_rel); }共用类及方法:public class ViewHelperUtils { /** * 给商品添加标签 * @param parentWidth 增加标签布局的宽度 * @param parentHeight 增加标签布局的高度 * @param tagsVoList 标签列表 * @param context * @param bitmapUtils * @param relativeLayout 增加标签的布局对象 */ public static void addProductTags(int parentWidth, int parentHeight, List<EMLayoutModulesTagsVo> tagsVoList, Context context, BitmapUtils bitmapUtils,RelativeLayout relativeLayout){ for (LayoutModulesTagsVo tagsVo : tagsVoList){ int tagwidth = (int) (tagsVo.getWidthProportion()*(parentWidth/100.00));// 宽度 int tagheight = (int) (tagsVo.getHeightProportion()*(parentHeight/100.00));// 高度 // 两者取最小保持一致 if (tagwidth>tagheight){ tagwidth = tagheight; } if (tagsVo.getPositionType().equals("LeftTop")){// 左上角 RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams( tagwidth, tagheight ); ImageView img = new ImageView(context); img.setLayoutParams(p); bitmapUtils.display(img,tagsVo.getTagImageUrl()); relativeLayout.addView(img); }else if(tagsVo.getPositionType().equals("RightTop")){// 右上角 RelativeLayout.LayoutParams p2 = new RelativeLayout.LayoutParams( tagwidth, tagheight ); p2.addRule(RelativeLayout.ALIGN_PARENT_TOP); p2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); ImageView img2 = new ImageView(context); //设置图片的位置 img2.setLayoutParams(p2); bitmapUtils.display(img2,tagsVo.getTagImageUrl()); relativeLayout.addView(img2); }else if(tagsVo.getPositionType().equals("LeftBottom")){// 左下角 RelativeLayout.LayoutParams p3 = new RelativeLayout.LayoutParams( tagwidth, tagheight ); p3.addRule(RelativeLayout.ALIGN_PARENT_LEFT); p3.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); ImageView img3 = new ImageView(context); //设置图片的位置 img3.setLayoutParams(p3); bitmapUtils.display(img3,tagsVo.getTagImageUrl()); relativeLayout.addView(img3); }else if(tagsVo.getPositionType().equals("RightBottom")){// 右下角 RelativeLayout.LayoutParams p4 = new RelativeLayout.LayoutParams( tagwidth, tagwidth ); p4.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); p4.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); ImageView img4= new ImageView(context); //设置图片的位置 img4.setLayoutParams(p4); bitmapUtils.display(img4,tagsVo.getTagImageUrl()); relativeLayout.addView(img4); } } } }