android客户端学习-RelativeLayout布局动态在四个角的位置增加标签

目前公司有一个商品图片动态增加标签的功能,标签为四张图片,根据不同的设置放在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);
            }
        }
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值