gpt4 book ai didi

android - 如何减小 Android 4.0 中的 TextView 文本大小?

转载 作者:行者123 更新时间:2023-11-29 22:05:11 25 4
gpt4 key购买 nike

(编辑:问题已解决。请参阅下面的答案)

我被这个问题困住了,它阻止了一个版本,所以我们将不胜感激每一个帮助。

下面是一个演示该问题的简单程序。我使用最新的 SDK (R19) 构建了它。它在 Gingerbread 上运行良好,但在 ICS 上运行失败。当文本大小增加(10 -> 30 -> 50)时,文本字段的高度按预期增长,但当文本大小减小(50 -> 10)时,文本字段具有完整高度,就好像文本大小是50.

有人能复制吗?有什么解决方法可以使减小文本大小正常工作的建议吗?

主要 Activity : 包测试.resize;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class ResizeTestActivity extends Activity {
private int mNextTextSize = 10;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final TextView textView = (TextView) findViewById(R.id.text0);

findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Cycle to next text size in {10, 30, 50}
textView.setText("Text Size " + mNextTextSize);
textView.append("\uFEFF"); // <--- THE FIX (see answer below)
textView.setTextSize(mNextTextSize);
// textView.requestLayout(); // does not help
// textView.invalidate(); // does not help
mNextTextSize = (mNextTextSize >= 50) ? 10 : (mNextTextSize + 20);
}
});
}
}

主要布局:

<?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="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click To Cycle Text Size" />

<TextView android:id="@+id/text0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dip"
android:background="#ffffff00"
android:text=""/>
</LinearLayout>

list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.resize"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".ResizeTestActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

The text field at original text size 10

The text field at text size 10 after cycling through size 50

最佳答案

回答我自己的问题。显然这是一个已报告的错误。

http://code.google.com/p/android/issues/detail?id=22493

添加

 tv.append("\uFEFF");  // zero width space

设置 TextView 文本后问题解决。

注意:如果您使用自定义字体,请确保它包含\uFEFF 字符。如果没有,您可能还会遇到单行省略号的问题。

关于android - 如何减小 Android 4.0 中的 TextView 文本大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10908150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com