TextView首行缩进效果(商品标签)

这里用两个TextView分别展示商品标签和商品描述信息.
对商品描述信息的TextView进行首行缩进处理.



通过LeadingMarginSpan.Standard(marginSpanSize, 0)设置首行缩进,这里第一个参数为首行缩进的距离,第二个参数为其余行缩进距离.

  /**
         * Constructor taking separate indents for the first and subsequent
         * lines.
         *
         * @param first the indent for the first line of the paragraph
         * @param rest the indent for the remaining lines of the paragraph
         */
        public Standard(int first, int rest) {
            mFirst = first;
            mRest = rest;
        }

首行缩进的距离距离为商品标签显示宽度.

      tvDes.setText(getSpannableString(label, description));
      tvLabel.setText(label);

    /**
     * 首行缩进的SpannableString
     *
     * @param label 标签信息
     * @param description 描述信息
     */
    private SpannableString getSpannableString(String label, String description) {
        SpannableString spannableString = new SpannableString(description);
        int marginSpanSize = (int) (label.length() * getResources().getDimension(R.dimen.label_size)
            + dp2px(this, 6));//文字宽度+ 背景padding4dp+间隔2dp
        LeadingMarginSpan leadingMarginSpan = new LeadingMarginSpan.Standard(marginSpanSize, 0);//仅首行缩进
        spannableString.setSpan(leadingMarginSpan, 0, description.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

布局文件



  
  



drawable/shape_bg_label



  
  
  

你可能感兴趣的:(TextView首行缩进效果(商品标签))