Android layout Android setTextSize TextView移动基线并切断文本高度

Android layout Android setTextSize TextView移动基线并切断文本高度,android-layout,Android Layout,我正在尝试使用背景图像调整TextView的大小。我有一个扩展TextView类的类,正在添加: MyCustomTextView tv2 = new MyCustomTextView(this); RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,

我正在尝试使用背景图像调整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的大小,但将文本变小将保持与变大时相同的基线。它开始从下到上剪切我的文本

我的问题是,如何移动基线,以便在调整大小时底部不会被切断


提前感谢。

通过添加构造函数修复了它。设置垂直滚动条效果很好

    this.setVerticalScrollBarEnabled(true);
    this.setMovementMethod(new ScrollingMovementMethod());
通过将缓冲区类型设置为Spanable,解决了我的(非)移动基线问题。这可以通过两种方式实现。在xml中设置缓冲区类型:

android:bufferType="spannable"
或调用代码中具有适当缓冲区类型的集合文本:

setText(getText(),BufferType.SPANNABLE);

通过将BufferType设置为Spanable,TextView将相应地自动重置基线。这将以非常小的性能成本实现。

对我来说非常有用!这到底是怎么回事呢?@Glenn之所以有效,是因为通过调用setMovementMethod,您无意中设置了android:bufferType=“spannable”,这需要一个不断变化的基线。