How to decrease TextView text size in Android 4.0?

北战南征 提交于 2019-12-13 02:28:27

问题


(EDIT: problem fixed. See answer below)

I am stuck with this issue and it blocks a release so every help will be greatly appreciated.

Below is a trivial program that demonstrates the issue. I built it with the latest SDK (R19). It works well on gingerbread but fails on ICS. When the text size is increased (10 -> 30 -> 50) the text field height grows as expected but when the text size is reduced (50 -> 10), the text field has the full height, as if the text size is 50.

Anybody can reproduce it? Any suggestion for a workaround to make reduction in text size working properly?

Main activity: 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);
            }
        });
    }
}

Main layout:

<?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>

Manifest:

<?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>


回答1:


Answering my own question. Apparantly this is a reported bug.

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

Adding

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

after setting the TextView text solves the problem.

Note: if you are using custom font, make sure it contains the \uFEFF character. If not, you may also have problems with single line ellipsis.




回答2:


Add textView.requestLayout() or textView.invalidate() after setting the new text size. I'm fairly certain one of those should do the trick.



来源:https://stackoverflow.com/questions/10908150/how-to-decrease-textview-text-size-in-android-4-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!