写文字在Android模拟器中的方法

法一:

TextView tv = new TextView(this);
tv.setText("你好");
setContentView(tv);

法二:

<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="你好,世界"
/>

 

 

改变字体的颜色的三种方法

法一(将整个字体改成一个颜色):

在main.xml中写

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="50sp"
android:textColor="#00ffff"
android:text="你好,世界"
/>

法二(可以设置不同颜色的字体):

/**
* testview是View的子类
* findViewById返回的是VIEW对象
* */

TextView tv = (TextView)findViewById(R.id.tv);
tv.setText(Html.fromHtml("我<font color = red>勒个去</font>啊"));

main.xml配置文件:

<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="50sp"
android:textColor="#00ffff"
android:text="你好,世界"
/>


法三(可以设置不同颜色的字体):

TextView tv = (TextView)findViewById(R.id.tv);
String str = "我勒个去";

//创建一个style对象
SpannableStringBuilder style = new SpannableStringBuilder(str);

style.setSpan(new ForegroundColorSpan(Color.WHITE), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
style.setSpan(new ForegroundColorSpan(Color.BLUE), 1, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
style.setSpan(new ForegroundColorSpan(Color.RED), 3, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(style);

main.xml配置文件:

<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="50sp"
android:textColor="#00ffff"
android:text="你好,世界"
/>

 



普通结构的Android XML布局文件是简单的.它是一个标记树、每一个标记都是一个试图类.在这个例子中,他是一个非常简单的树,只有一个元素TextView。你可以在你的布局文件中使用任何试图派生类的名字作为你的标记名,包括在你自己代码中定义的视图类。这种结构使得创建许多用户界面变得非常简单,只需要使用一个简单的结构和语法,相比较使用代码实现要容易得多。这个模型是从web开发模型中得到的灵感,在web开发中,你可以从你的应用程序逻辑中分离用户界面,然后再组合起来并填充数据。(This model is inspired by the web development model, where you can separate the presentation of your application (its UI) from the application logic used to fetch and fill in data.这句话不是很明白,所以翻译很牵强)
In this example, there are also four XML attributes. Here's a summary of what they mean:
这个例子中,有四个XML属性。下面是它们含义的概述:


 

因此,这就使XML布局看起来的样子,但是它们放在什么地方呢?就在你的工程的res/路径下。”res”是”resources”的简写,它包含你的项目需要的所有非代码资源,包括图像,本地化字符串和XML布局文件。


现在打开包浏览器中源代码文件夹中的名为R.java的文件,你将看到如下的代码。
public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int icon=0x7f020000;
    }
    public static final class layout {
        public static final int main=0x7f030000;
    }
    public static final class string {
        public static final int app_name=0x7f040001;
        public static final int hello=0x7f040000;
    }
}

一个工程的R.java文件定义了所有资源的索引。在你的代码中使用这个类作为你个比较快捷的方法去访问你的工程能够中的资源。对于像Eclipse这样子具有自动生成代码特性的IDE来说尤为功能强大,因为它可以让你快速和交互地定位你搜索的特定引用。
最重要的事情是注意到的事名为”layout”的内部类, 和它的成员域”main”。Eclipse插件会侦测到你添加了一个新的XML布局文件和重新生成的这个R.java文件,当你添加了新资源到你的项目中之后,你会发现R.java自动跟新。
最后要做的就是使用你的UI的新版本的XML文件修改你的HelloAndroid源码,以取代纯粹使用代码来修改。下面是你的新类看上去的样子,正如你所见,源码变得十分简单。

public class HelloAndroid extends Activity

{ /** Called when the activity is first created. */

@Override
public void onCreate(Bundle icicle)

{ super.onCreate(icicle);

setContentView(R.layout.main);

}

 

 

 

将用户界面转化成XML布局
你刚刚完成的“Hello,World”的例子使用了我们称作”programmatic”的UI布局。这意味着你直接使用代码来构造和创建应用程序用户界面。如果你完成大量的用户界面编程,你可能会意识到这种方法有时是多么的脆弱:一个版面小小的改动导致大量的源码问题.同时也非常容易忘记连接适当的视图,这都会导致你的版面错误,并且浪费大量的时间来调试代码。
这就是为什么Android提供一个动态的UI 构造模型:基于XML的布局文件。

 

main.xml配置文件:

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="50sp"
android:textColor="#00ffff"
android:text="你好,世界"
/>