Android textview文本在自定义字体的两侧被切断

Android textview文本在自定义字体的两侧被切断,第1张

概述这是在预览和设备上发生的事情:TextView没什么特别的,只是加载自定义字体:publicclassTestTextViewextendsAppCompatTextView{publicTestTextView(Contextcontext){super(context);init(context);}publicTestTextView(Contextcont

这是在预览和设备上发生的事情:


TextVIEw没什么特别的,只是加载自定义字体:

public class TestTextVIEw extends AppCompatTextVIEw {    public TestTextVIEw(Context context) {        super(context);        init(context);    }    public TestTextVIEw(Context context, AttributeSet attrs) {        super(context, attrs);        init(context);    }    public TestTextVIEw(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        init(context);    }    voID init(Context context) {        Typeface t = Typeface.createFromAsset(context.getAssets(), "Fonts/daisy.ttf");        setTypeface(t);    }}

布局也很基础,但以防万一:

<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:app="http://schemas.androID.com/apk/res-auto"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:background="@color/material_red200"    androID:orIEntation="vertical">        <*custompackage* .TestTextVIEw        androID:gravity="left"        androID:padding="0dp"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:text="just some text for testing"        androID:textcolor="@color/material_black"        androID:textSize="100dp" /></linearLayout>

如您所见,左侧部分,如“j”和“f”被切断.

设置填充或边距不起作用.

从其他程序使用时,此字体适合其框架.

提前致谢.

编辑:
在我的案例中,@ play_err_提到的不是解决方案.

>我在最终版本中使用自动调整大小的textvIEw,因此添加空格会非常困难.
>我需要解释为什么其他程序(例如photoshop,后效…)可以计算出一个合适的边界框而androID不能
>我也在动态加载不同的字体,我不想创建一个

if(badFont)     addspaces()

解决方法:

这个答案让我走上了正确的道路:
https://stackoverflow.com/a/28625166/4420543

因此,解决方案是创建自定义TextvIEw并覆盖onDraw方法:

    @OverrIDe    protected voID onDraw(Canvas canvas) {        final Paint paint = getPaint();        final int color = paint.getcolor();        // Draw what you have to in transparent        // This has to be drawn, otherwise getting values from layout throws exceptions        setTextcolor(color.transparent);        super.onDraw(canvas);        // setTextcolor invalIDates the vIEw and causes an endless cycle        paint.setcolor(color);        System.out.println("Drawing text info:");        Layout layout = getLayout();        String text = getText().toString();        for (int i = 0; i < layout.getlineCount(); i++) {            final int start = layout.getlinestart(i);            final int end = layout.getlineEnd(i);            String line = text.substring(start, end);            System.out.println("line:\t" + line);            final float left = layout.getlineleft(i);            final int baseline = layout.getlineBaseline(i);            canvas.drawText(line,                    left + getTotalpaddingleft(),                    // The text will not be clipped anymore                    // You can add a padding here too, faster than string string concatenation                    baseline + getTotalpaddingtop(),                    getPaint());        }    }
总结

以上是内存溢出为你收集整理的Android textview文本在自定义字体的两侧被切断全部内容,希望文章能够帮你解决Android textview文本在自定义字体的两侧被切断所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1098271.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存