[英]Android Clip/Cut off characters in TextView
I've got a textview as a single line in a GridLayout. 我在GridLayout中将textview作为单行显示。 It should fill out the whole screen in width. 它应填满整个屏幕的宽度。
So when I click a button I append a new character to the textview with term.append(String.valueOf(value));
因此,当我单击按钮时,我用term.append(String.valueOf(value));
将新字符追加到term.append(String.valueOf(value));
Where term
is the textview. term
是textview。
But there is no clipping, means I can fill the textview with a lot of characters over the actual size of the TexView. 但是没有剪裁,这意味着我可以在TexView的实际大小上用很多字符填充textview。 I want to stop adding characters after the textview is full. 我想在textview已满后停止添加字符。 Alternatively I could horizontal scrolling, but that doesn't work neither. 另外,我可以水平滚动,但这都不起作用。
I tried the following code: 我尝试了以下代码:
Paint p = new Paint();
if (term.getMaxWidth() > p.measureText(term.getText().toString()))
term.append(String.valueOf(value));
but it the value for term.getMaxWidth() (same for LayoutParams().width) is way too high. 但是term.getMaxWidth()的值(与LayoutParams()。width相同)太高了。
This is my style xml: 这是我的样式xml:
<GridLayout
android:layout_width="368dp"
android:layout_height="495dp"
android:layout_marginTop="8dp"
android:columnCount="4"
android:alignmentMode="alignMargins"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="16dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="16dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:foregroundGravity="center"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintVertical_bias="0.0">
<TextView
android:id="@+id/textView3"
android:layout_width="355dp"
android:maxWidth="355dp"
android:layout_height="62dp"
android:layout_columnSpan="4"
android:text="@string/calc_term"
android:textSize="30sp"
android:maxLines="1"
android:scrollHorizontally="false"
android:isScrollContainer="false"
android:ellipsize="start" />
Thank you 谢谢
Try like this: 尝试这样:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="300dp"
Ok I got a solution by myself: The Paint object doesn't know about the scaling of the text. 好的,我自己解决了一个问题:Paint对象不知道文本的缩放比例。 Therefore I need to use the TextViews own Paint object. 因此,我需要使用TextViews自己的Paint对象。
TextPaint paint = term.getPaint();
if (term.getWidth() > paint.measureText(term.getText().toString())
+ paint.measureText(String.valueOf(value)))
term.append(String.valueOf(value));
does the trick. 绝招。 Where value
is the new character. value
是新角色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.