gpt4 book ai didi

android - 我如何计算 TextView 的字符限制?

转载 作者:行者123 更新时间:2023-11-30 04:56:03 25 4
gpt4 key购买 nike

我想计算 TextView 可以容纳的字符总数,文本大小为 14dp,宽度为 100dp,高度为 100dp。然后我必须将这些字符的限制添加到 EditText。

 <TextView
android:id="@+id/OFSO_text1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_weight="1"
android:layout_gravity="top|center"
android:fontFamily="@font/lato_bold"
android:gravity="center|bottom"
android:text="SEASONAL DISCOUNT ARE HERE"
android:textAlignment="center"
android:textAllCaps="true"
android:textColor="@color/lightcream"
android:textSize="14dp"
android:textStyle="normal"
android:autoSizeTextType="none"
/>

我试过这段代码,但它不能正常工作。这为我提供了比此 Textview 可以容纳更多的字符限制。

            TextPaint paint = textView.getPaint();
int wordwidth=(int)paint.measureText("a",0,1);
int screenwidth = getResources().getDisplayMetrics().widthPixels;
int maxLength = screenwidth/wordwidth;

请帮帮我!!提前致谢。快乐编码!!!

最佳答案

您的测量值中缺少字体大小和字体。

        // In case you are storing your font in res/font
Typeface typeface = ResourcesCompat.getFont(context, R.font.noto_sans_regular);
paint.setTypeface = typeface
paint.setTextSize = sizeOfYourFontInPixels

关于android - 我如何计算 TextView 的字符限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59133949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com