我有一个TextView,其高度是动态的.我想设置如果无法完全绘制最后一行是不可见的. 截图
高度是动态的原因是它在AppWidget中使用,并且appwidget的大小在不同的设备上是不同的.因此,您无法测量TextView或无法使用TextView的子类. 链接
这就是我现在基本上使用的.当我将长文本放入TextView时,最后一行看起来很难看.
<LinearLayout
android:id="@+id/widgetContent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/sg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我最近遇到了这个问题并使用了另一种解决方案.
我正在动态设置maxLines:
final TextView tv = ((TextView) myLayout.findViewById(R.id.mytextview));
tv.setText(value);
ViewTreeObserver vto = tv.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
private int maxLines = -1;
@Override
public void onGlobalLayout() {
if (maxLines < 0 && tv.getHeight() > 0 && tv.getLineHeight() > 0) {
int height = tv.getHeight();
int lineHeight = tv.getLineHeight();
maxLines = height / lineHeight;
tv.setMaxLines(maxLines);
}
}
});
Run Code Online (Sandbox Code Playgroud)