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;
    }

布局文件

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal"
  android:padding="10dp">
  <TextView
    android:id="@+id/des_tv"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:text=""
    />
  <TextView
    android:layout_marginTop="4dp"
    android:id="@+id/label_tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="@dimen/label_size"
    android:textColor="#FFFFFF"
    android:background="@drawable/shape_bg_label"
    android:text=""
    />
</FrameLayout>

drawable/shape_bg_label

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">
  <solid android:color="#FF0000"></solid>
  <corners android:radius="3dp"></corners>
  <padding
    android:bottom="0dp"
    android:left="2dp"
    android:right="2dp"
    android:top="0dp"></padding>
</shape>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值