//src../../MainActivity.java
package com.edwin.textview.activity;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.Menu;
import android.widget.TextView;
//import android.widget.TextView;
// ctrl+/注释和取消注释
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//TextView tv = new TextView(this);
//tv.setText("你好");
//setContentView(tv);
TextView tv = (TextView)findViewById(R.id.tv);
//tv.setText(Html.fromHtml("欢迎大家收看<font color=blue>NBA</font>谢谢"));
String str= "大家好,我是edwin,我的手机号码是139592xxxxx,我的博客是http://www.Edwin.com,请多关注";
SpannableStringBuilder style =new SpannableStringBuilder(str);
style.setSpan(new ForegroundColorSpan(Color.RED), 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
style.setSpan(new ForegroundColorSpan(Color.GREEN), 2, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
style.setSpan(new ForegroundColorSpan(Color.BLUE), 5, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(style);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
//res/layout/mainActivity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv"
android:autoLink="all"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="@string/hello_world"
/>
</RelativeLayout>
// res/values/string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">2.TextView</string>
<string name="action_settings">Settings</string>
<string name="hello_world">大家好,我是edwin,\n我的手机号码是139592xxxxx,我的博客是http://www.edwin.com</string>
</resources>