【问题标题】:Android:TextView height doesn't change after shrinking the font sizeAndroid:缩小字体大小后TextView高度不会改变
【发布时间】:2012-03-02 22:22:47
【问题描述】:

当我点击按钮时,字体大小缩小到 12。 但是,结果是:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="40sp"
        android:background="#80ff0000"
        android:text="@string/hello" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

java:

public class FontSizeTestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final TextView text = (TextView) findViewById(R.id.test);




        Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                text.setTextSize(12);
            }
        });
    }
}

如何缩小 textView 的高度,使其只包裹实际字体?

【问题讨论】:

  • 我刚刚测试了您的代码,它可以正常工作。你用的是哪个版本的安卓?
  • 我在 Android 3.2 & 4.0 上测试过
  • 这些解决方案都不适用于任何版本的 Android。 T__________T

标签: android textview


【解决方案1】:

所以,经过长时间的搜索,我找到了解决方案。 每次设置文本时:

setText("I am a Text",TextView.BufferType.SPANNABLE);

或者在调整文本大小之后:

setText(getText(),TextView.BufferType.SPANNABLE);

【讨论】:

  • 很好,已接受的答案
  • 非常简单,非常愚蠢。已经花了很多时间(我认为太多了),但您节省了更多...
【解决方案2】:

终于找到原因/解决办法了!!!

这是 Android 3.1+ 的已知错误

Issue 17343

Issue 22493

可能的解决方法是:

text.setText(text+"\n");

final String DOUBLE_BYTE_SPACE = "\u3000";
text.setText(text + DOUBLE_BYTE_SPACE);

【讨论】:

  • 很好地发现了这些问题。使用提到的 \u3000 或 \u2060 会在字符串末尾产生一个空格或“框”字符,具体取决于设备字体。所以只是附加一些东西是不可靠的。
  • Moritz 的回答更正确:添加“\u200b”(它不会为某些设备创建破盒)
【解决方案3】:

而不是使用:

final String DOUBLE_BYTE_SPACE = "\u3000";
text.setText(text + DOUBLE_BYTE_SPACE);

更好的使用:

final String DOUBLE_BYTE_WORDJOINER = "\u2060";
text.setText(text + DOUBLE_BYTE_WORDJOINER);

额外信息:Word Joiner 是 0 宽度空间。

【讨论】:

  • 这对我来说非常有效,而添加“\n”却没有。
  • 谢谢,为什么我以前没有看到这个? android 文档中是否对此有任何说明?
  • 那是行不通的。导致2个问题。如果您将文本右对齐或居中,您会看到“0 宽度”在大多数设备上是神话。在搭载 Android 2.3.3 的三星 Galaxy S2 上,它甚至显示为可见的矩形。
【解决方案4】:

我发现添加字符“\u200b”不会显示为断字符,也不会添加空格/换行符。

有关“零宽度空间”的更多详细信息,请参阅:http://www.fileformat.info/info/unicode/char/200b/index.htm

【讨论】:

  • 谢谢!也在与破碎的角色作斗争,但这一个有效。
【解决方案5】:

也许将视图设置为 View.GONE,更改文本大小,然后设置为 View.VISIBLE 是否可行?

【讨论】:

  • 以上答案都不适合我,但确实如此。
【解决方案6】:

尝试调用 invalidate() 或 invalidate() 的变体。 来自 android 开发者文档:

公共无效无效()

自:API 级别 1 使整个视图无效。如果视图可见,onDraw(android.graphics.Canvas) 将在未来的某个时间点被调用。这必须从 UI 线程调用。要从非 UI 线程调用,请调用 postInvalidate()。

http://developer.android.com/reference/android/view/View.html#invalidate()

【讨论】:

    【解决方案7】:

    在 main.xml 文件中给定的 TextView 大小 android:textSize="40sp" 。并在 java 中,单击按钮更改此处的值

      public void onClick(View v) {
    
                text.setTextSize(12);
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-03
      • 1970-01-01
      • 2012-02-05
      • 1970-01-01
      • 2018-09-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多