Android的setTextSize TextView的移动基线和切断文字高度 [英] Android setTextSize TextView moves baseline and cuts off text height

查看:708
本文介绍了Android的setTextSize TextView的移动基线和切断文字高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图调整与背景图像一个TextView。我有一个扩展的TextView类的类,并且正在加入这样的:

I am trying to resize a TextView with the background image. I have a class that extends the TextView class, and is being added as such:

    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类:

and I am zooming like so in my custom TextView class:

    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的,但让文字较小将保持相同的基本线时,它是很大的。它开始从下往上砍我的文字了。

What ends up happening is that making the text bigger will correctly resize the TextView, but making the text smaller will keep the same base line as when it was big. It starts to cut my text out from the bottom up.

我的问题是,我怎么能移动基准线,使其底部没有得到调整,同时切断了。

My question is, how can I move the base line so the bottom does not get cut off while resizing.

先谢谢了。

推荐答案

我的(非)移动基线问题通过设置缓冲区类型spannable解决。这可以通过两种方式来完成。无论是设置缓冲区类型的XML:

My (non)shifting baseline problem was solved by setting the buffer type to spannable. This can be done in two ways. Either set the buffer type in xml:

android:bufferType="spannable"

或致电与code中的适当的缓冲类型设置文本:

or call set text with the appropriate buffer type in code:

setText(getText(),BufferType.SPANNABLE);

通过设置BufferType到spannable,TextView的都会自动重置基线。这会在一个非常小的性能开销。

By setting the BufferType to spannable, the TextView will automatically reset the baseline accordingly. This will come at a very minor performance cost.

这篇关于Android的setTextSize TextView的移动基线和切断文字高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆