相关疑难解决方法(0)

TextView - 以编程方式设置文本大小似乎不起作用

我正在使用Eclipse Indigo,在2个模拟器(2.2和3.0)上进行测试.

下面的代码显示了我现在正在测试的内容,但是设置文本大小时,在尝试运行模拟器时屏幕上没有显示任何内容.(如果我注释掉文本大小,文本显示为红色).我认为不知何故eclipse并没有重建代码,但我添加了代码行来添加蓝色背景并且工作正常.我在设置文本后尝试设置文本大小仍然没有成功.代码如下.谢谢你的帮助!(免责声明) - 我试图远离xml.因为我已经知道java我不想依赖它.

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);
  }
}
Run Code Online (Sandbox Code Playgroud)

android

126
推荐指数
6
解决办法
17万
查看次数

将像素转换为sp

我需要在当前TextSizeTextViewsp单位.

但是getTextSize()返回大小pixels.那有办法convert pixels to sp吗?

android textview

53
推荐指数
3
解决办法
6万
查看次数

标签 统计

android ×2

textview ×1