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 :點擊打開鏈接