的TextView - 编程设置文字大小似乎不工作文字大小、工作、TextView

2023-09-04 09:52:26 作者:阴霾-

我使用的Eclipse靛蓝,测试2仿真器(2.2和3.0)。

在code以下显示了我测试了,但是设置尝试运行仿真器时的文本大小显示没有在屏幕上。(如果我注释掉的文本大小的文本显示了一个红色) 。我想,不知怎的,日食并没有重建code,但我添加了code线添加了蓝色背景和工作。我曾尝试与仍然没有成功设置文本后设置文字大小。在code是如下。感谢你的帮助! (免责声明) - 我试图远离XML。是,我已经知道java的,我不希望依赖于这一点。

 进口android.app.Activity;
进口android.graphics.Color;
进口android.os.Bundle;
进口android.widget.TextView;

公共类TestAndroidvs2Activity延伸活动{
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    TextView的文本=新的TextView(本);
    text.setTextColor(Color.RED);
    text.setTextSize(2);
    text.setBackgroundColor(Color.BLUE);
    text.setText(你好Android的);


    的setContentView(文本);
  }
}
 

解决方案

文字大小2将几乎不可见。 14试试看,至少。顺便说一句,使用XML有许多优点,将让您的生活更轻松,一旦你需要做的事情比世界你好更加复杂。

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

从零开始学Android之TextView

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);
  }
}

解决方案

Text size 2 will be practically invisible. Try it with 14 at least. BTW, using xml has a lot of advantages and will make your life easier once you need to do anything more complex than 'Hello World'.