(編輯:問題修復,見下面的答案)如何減少Android 4.0中的TextView文字大小?
我被這個問題困住了,它阻止了一個發佈,所以每一個幫助將不勝感激。
下面是一個簡單的程序,演示了這個問題。我用最新的SDK(R19)構建它。它在薑餅上效果很好,但在ICS上失敗。當文本大小增加(10 - > 30 - > 50)時,文本字段高度會按預期增長,但當文本大小減小(50 - > 10)時,文本字段具有完整高度,就好像文本大小是50.
有人可以重現嗎?任何解決方案的建議,以減少文字大小正常工作?
主要活動: package test.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>
清單:
<?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>
謝謝kcoppock。只是再次嘗試(每一個都),但沒有幫助。更新了問題以反映它。 – user1139880