【发布时间】:2019-01-29 15:15:26
【问题描述】:
我有这段代码。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.8"
android:orientation="vertical"
android:id="@+id/textLayout"
android:background="@drawable/vdaybg"
android:gravity="center"
android:layout_gravity="center">
<TextView
android:id="@+id/detail_text"
android:text="hello"
android:background="#55000000"
android:lineSpacingExtra="5dp"
android:textSize="30dp"
android:textColor="@android:color/white"
android:layout_margin="10dp"
android:layout_gravity="center"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
</TextView>
</LinearLayout>
我正在从 Activity 动态设置文本
detailText.setText(msg);
detailText.setTypeface(Utils.typeFaceBold);
现在由于我设置了多行文本,因此较大的行换行到下一行。 我想阻止这种情况。 当线条即将变形时,我想减小字体大小。
目前我已经找到了执行此操作的这段代码
public void correctWidth(TextView textView, int desiredWidth) {
Paint paint = new Paint();
Rect bounds = new Rect();
paint.setTypeface(textView.getTypeface());
float textSize = textView.getTextSize();
paint.setTextSize(textSize);
String text = textView.getText().toString();
paint.getTextBounds(text, 0, text.length(), bounds);
while (bounds.width() > desiredWidth)
{
textSize--;
paint.setTextSize(textSize);
paint.getTextBounds(text, 0, text.length(), bounds);
}
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
}
但是在desiredWidth 中传递什么?它会起作用吗?
【问题讨论】:
标签: android