Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
449 views
in Technique[技术] by (71.8m points)

android - TextView - setting the text size programmatically doesn't seem to work

I am using Eclipse Indigo, testing on 2 emulators(2.2 and 3.0).

the code below shows what I am testing now, however setting the text size reveals nothing on the screen when trying to run the emulator.(if i comment out the text size the text shows up with a red color). I thought that somehow eclipse wasn't rebuilding the code but i added the line of code to add the blue background and that worked. I have tried setting the text size after setting the text with still no success. the code is below. thanks for your help! (disclaimer) - i am trying to stay away from xml. Being that i already know java i don't want to depend on that.

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;

public class TestAndroidvs2Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TextView text = new TextView(this);
    text.setTextColor(Color.RED);
    text.setTextSize(2);    
    text.setBackgroundColor(Color.BLUE);
    text.setText("Hello Android");


    setContentView(text);
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

the method TextView.setTextSize(int unit , float size); takes two parameters .

Try this :

text.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);

refer this and this.

UPDATE: Now the setTextSize(float size) will set the text size automatically in "scaled pixel" units. no need to mention the COMPLEX_UNIT_SP manually. Refer to the documentation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...