private void drawImageViewDone(int width, int height) {
try
{
textWidth = content_top_tv.getTextSize();
paint.setTextSize(textWidth);
// 一行字体的高度
int lineHeight = content_top_tv.getLineHeight();
// 可以放多少行
int lineCount = (int) height / lineHeight;
// int lineCount = 4;
// 一行的宽度
float rowWidth = mScreenWidth - width - content_top_tv.getPaddingLeft()
- content_top_tv.getPaddingRight();
// 一行可以放多少个字
int columnCount = (int) (rowWidth / textWidth);
// 总共字体数等于 行数*每行个数
count = lineCount * columnCount;
if(text.length() < count)
{
content_top_tv.setText(text);
content_bottom_tv.setVisibility(View.GONE);
return;
}
// 一个TextView中所有字符串的宽度和(字体数*每个字的宽度)
textTotalWidth = (float) ((float) count * textWidth);
measureText();
content_top_tv.setText(text.substring(0, count));
// 检查行数是否大于设定的行数,如果大于的话,就每次减少一个字符,重新计算行数与设定的一致
while (content_top_tv.getLineCount() > lineCount) {
count -= 1;
content_top_tv.setText(text.substring(0, count));
}
//content_bottom_tv.setPadding(0, lineCount * lineHeight - height, 0, 0);
//gz此处修改将原有布局中marginTop=8dp 删除,setPadding顶部取的值由 -8 改为 8, 从而解决textview顶部被砍掉的错乱问题
content_bottom_tv.setPadding(0, height-lineCount * lineHeight , 0, 0);
content_bottom_tv.setText(text.substring(count));
}
catch(Exception e)
{
e.printStackTrace();
}
}
xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/preg_export_bg"
android:orientation="horizontal"
android:padding="6dip" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/epic"
android:layout_width="90dip"
android:layout_height="90dip"
android:layout_alignParentRight="true"
android:layout_marginRight="6dip"
android:layout_marginTop="5dip"
android:background="@drawable/default_user_head" />
<TextView
android:id="@+id/content_top_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="6dip"
android:layout_marginTop="6dip"
android:layout_toLeftOf="@+id/epic"
android:lineSpacingExtra="4dip"
android:textColor="@color/gray3"
android:text="打算发送到发送到发送到发送到发送到发送到发送到发的说法送到发送到asdfasdf发的说法gadf "
android:textSize="16sp" />
<TextView
android:id="@+id/content_bottom_tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/epic"
android:layout_marginLeft="6dip"
android:layout_marginRight="6dip"
android:lineSpacingExtra="4dip"
android:textColor="@color/gray3"
android:text="打算发送到发送到发送到发送到发送到发送到发送到发的说法"
android:textSize="16sp" />
</RelativeLayout>
</LinearLayout>
demo :点击打开链接