这里用两个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>
网友评论