我正在尝试使用背景图像调整 TextView 的大小。我有一个扩展 TextView 类的类,并按如下方式添加:
MyCustomTextView tv2 = new MyCustomTextView(this);
RelativeLayout.LayoutParams lparams = new
RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
lparams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
tv2.setLayoutParams(lparams);
myLayout.addView(v);
我在自定义 TextView 类中像这样缩放:
public float getScaledTextHeight()
{
float textHeightDPI = initial_size/windowHeight_inDPI;
float textOnScreenHeight = textHeightDPI * windowHeight_inDPI;
float scaledText = textOnScreenHeight * mZoom;
return scaledText;
}
public void setZoomTextHeight(float zoom)
{
mZoom = zoom;
image_size = getScaledTextHeight();
float mX = ((offset_x + img_offset_x) * mZoom);
float mY = ((offset_y + img_offset_y) * mZoom);
RelativeLayout.LayoutParams position1 =
(android.widget.RelativeLayout.LayoutParams)this.getLayoutParams();
position1.leftMargin = (int)mX;
position1.topMargin = (int)mY;
position1.bottomMargin = (int)(window_height - (position1.topMargin +
image_size + 16));
this.setLayoutParams(position1);
setTextSize(TypedValue.COMPLEX_UNIT_PX , image_size);
invalidate();
}
最终发生的情况是,增大文本将正确调整 TextView 的大小,但减小文本将保持与大时相同的基线。它开始从下往上剪切我的文本。
我的问题是,如何移动基线,以便在调整大小时底部不会被切断。
提前致谢。
我的(非)移动基线问题通过将缓冲区类型设置为可跨越来解决。这可以通过两种方式完成。在 xml 中设置缓冲区类型:
android:bufferType="spannable"
或在代码中使用适当的缓冲区类型调用设置文本:
setText(getText(),BufferType.SPANNABLE);
通过将 BufferType 设置为 spannable,TextView 将自动相应地重置基线。这将以非常小的性能成本为代价。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)