【发布时间】:2019-12-02 06:54:18
【问题描述】:
我想计算 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;
请帮帮我!!提前致谢。 编码愉快!!!
【问题讨论】:
-
试试 OFSO_text1.getText().toString().length();并将其存储到变量中
-
TextSize 应该在 SP ..
-
这能回答你的问题吗? Limit the Length of TextView
-
@Niceumang 我不想计算字符串字符,我想向这个 TextView 添加更多文本,但在此之前,我需要知道这个文本视图可以容纳多少个字符?明白我的意思了吗?
标签: android android-layout textview android-edittext