RelativeLayout 中自定义布局

2017-07-10  本文已影响271人  基本密码宋

来着 http://www.jianshu.com/p/1a5303258754

 /**
     * 初始化 左边的图片
     */
    private void initLeftIcon() {
        if (leftIconIV == null) {
            leftIconIV = new ImageView(mContext);
        }
        leftImgParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        leftImgParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        leftImgParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE); //左边
        leftImgParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE); //上下居中
        if (mLeftIconWidth != 0 && mLeftIconHeight != 0) {
            leftImgParams.width = mLeftIconWidth;
            leftImgParams.height = mLeftIconHeight;
        }
        leftIconIV.setScaleType(ImageView.ScaleType.FIT_CENTER);
        leftIconIV.setId(R.id.id_leftIcon);
        leftIconIV.setLayoutParams(leftImgParams);
        if (mLeftDrawable != null) {
            leftImgParams.setMargins(mLeftIconMarginLeft, 0, 0, 0); //设置边距
            leftIconIV.setImageDrawable(mLeftDrawable);
        }
        addView(leftIconIV);
    }
 /**
     * 初始化 右边的图片
     */
    private void initRightIcon() {
        if (rightIconIV == null) {
            rightIconIV = new ImageView(mContext);
        }
        rightImgParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        rightImgParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE); //左边
        rightImgParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE); //上下居中
        rightIconIV.setScaleType(ImageView.ScaleType.FIT_CENTER);
        rightIconIV.setId(R.id.id_rightIcon);
        rightIconIV.setLayoutParams(rightImgParams);
        if (mRightIconWidth != 0 && mRightIconHeight != 0) {
            rightImgParams.width = mRightIconWidth;
            rightImgParams.height = mRightIconHeight;
        }
        if (mRightDrawable != null) {
            rightImgParams.setMargins(0, 0, mRightIconMarginRight, 0); //设置边距
            rightIconIV.setImageDrawable(mLeftDrawable);
        }
        addView(rightIconIV);
    }

实在不想写了 真的好多...

https://github.com/jenosongjiabin/PopularDemo/tree/master/supertextview

上一篇下一篇

猜你喜欢

热点阅读