Android:缩小字体大小后,TextView高度不会改变

18
當我點擊按鈕時,字體大小會縮小到12號。但是結果是:

enter image description here

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的高度,使其只包裹实际字体?


我刚刚测试了你的代码,它按照预期工作。你使用的是哪个版本的安卓系统? - caiocpricci2
我在 Android 3.2 和 4.0 上进行了测试。 - jclova
这些解决方案在任何版本的Android上都对我无效。T__________T - Jimbali
7个回答

23

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

</p>
setText("I am a Text",TextView.BufferType.SPANNABLE);

或者在调整文本大小后,只需执行:

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

好的,已接受的答案。 - Muhammad Farhan Habib
非常简单,非常愚蠢。已经花费了很多时间(我认为太多了),但你节省了更多... - kpower

5

不要使用:

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 是零宽度空格。


1
这对我完美地起作用了,而添加“\n”则没有做到。 - tmanthey
谢谢,为什么我之前没看到这个?Android文档中有关于这个的注释吗? - deadfish
这并没有真正起作用。会导致两个问题。如果你将文本右对齐或居中,你会发现“0宽度”在大多数设备上是不存在的。在一台安卓2.3.3版本的三星Galaxy S2手机上,甚至会显示为可见的矩形。 - tmanthey

5

最终,我找到了原因/解决方案!!!

这是Android 3.1+已知的一个bug

问题17343

问题22493

可能的解决方法有:

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

或者

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

1
问题找到了,使用提到的 \u3000 或 \u2060 会在字符串末尾产生一个空格或“方框”字符,这取决于设备字体。因此,仅仅附加一些内容是不可靠的。 - Moritz
Moritz的回答更正确:添加“\u200b”(它不会为某些设备创建破碎的框) - jclova

3

谢谢!我也曾经苦恼于乱码问题,但这个方法有效。 - Gu1234

1
也许将视图设置为View.GONE,更改文本大小,然后再设置为View.VISIBLE会起作用吗?

以上的答案都不适用于我,但这个可以。 - daniyelp

1

尝试调用invalidate()或invalidate()的变体。

来自Android开发者文档:

public void invalidate()

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

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


0
在 main.xml 文件中,TextView 的大小被设置为 android:textSize="40sp"。在 Java 代码中,点击按钮可以改变这里的值。
  public void onClick(View v) {

            text.setTextSize(12);
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接