如何防止 TextView 在插入字符串时更改其大小? (Android)

How can I prevent a TextView from changing its size when inserting a String? (Android)

我在我的 android 应用程序中构建了一个方形井字棋场 填满整个屏幕宽度。

但每次我通过 setText 方法 将字符串 插入单元格时,它都会更改此单元格的大小(取决于插入的字符串的大小)。

如何在单元格 中插入(最大大小)String/char 而不更改其大小

代码比较长,有需要可以上传

你可以在xml中定义:

android:maxWidth="200dp"

或java:

textView.setMaxWidth(300);

您还可以使用 LinearLayoutTextView 指定屏幕的百分比,例如,如果我们有 3 列的 table,我们会:

<LinearLayout android:width="match_parent"
              android:height="wrap_content"
              android:orientation="horizontal"
              android:weightSum="3">

              <TextView android:width="0dp"
                        android:height="wrap_content"
                        android:weight="1"/>

              <TextView android:width="0dp"
                        android:height="wrap_content"
                        android:weight="1"/>

              <TextView android:width="0dp"
                        android:height="wrap_content"
                        android:weight="1"/>

</LinearLayout>

通过这样做,每个 TextView 将获得其父级 match_parent 宽度的三分之一的宽度。

根据您对其他答案的评论,我假设您的意思是 TextView 的高度正在扩大。 (multi-line)

要解决此问题,请在 xml 中使用以下内容:

android:singleLine="true"
android:ellipsize="end"

这会将您的 TextView 限制为单行,如果超过它,它将在末尾显示一个“...”。