Android 之 TextView的使用:
1.创建一个TextView控件:
第一种方法:在类中进行创建:
//创建一个TextView控件,然后显示在Activity上面
TextView tv = new TextView(this);
tv.setText("你好,这是一个TextView程序");
setContentView(tv);
第二种方法:在main.xml文件中创建(推荐使用)
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
2.改变字体颜色的三种方法:
第一种方法:
在main.xml中设置id:android:id="@+id/tv"
用于改变局部颜色
TextView tv = (TextView)findViewById(R.id.tv);
tv.setText(Html.fromHtml("在控件中添加一段话,<font color=blue>这段显示的就是蓝颜色的,</font>注意看!!"));
第二种方法:
另一种用于改变局部颜色的方法
TextView tv = (TextView)findViewById(R.id.tv);
String str = "红色,蓝色,绿色。";
SpannableStringBuilder style = new SpannableStringBuilder(str);
style.setSpan(new ForegroundColorSpan(Color.RED), 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
style.setSpan(new ForegroundColorSpan(Color.GREEN), 4, 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
style.setSpan(new ForegroundColorSpan(Color.BLUE), 6, 9, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(style);
第三种方法,在main.XML中进行设置:
android:textSize="20sp"
android:textColor="#00ff00"
3.设置超链接:
android:autoLink="all" 其中参数可以是:none,email, map ,web, phone, all
4.跑马灯设置:
android:focusable="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusableInTouchMode="true"
android:singleLine="true"
1.创建一个TextView控件:
第一种方法:在类中进行创建:
//创建一个TextView控件,然后显示在Activity上面
TextView tv = new TextView(this);
tv.setText("你好,这是一个TextView程序");
setContentView(tv);
第二种方法:在main.xml文件中创建(推荐使用)
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
2.改变字体颜色的三种方法:
第一种方法:
在main.xml中设置id:android:id="@+id/tv"
用于改变局部颜色
TextView tv = (TextView)findViewById(R.id.tv);
tv.setText(Html.fromHtml("在控件中添加一段话,<font color=blue>这段显示的就是蓝颜色的,</font>注意看!!"));
第二种方法:
另一种用于改变局部颜色的方法
TextView tv = (TextView)findViewById(R.id.tv);
String str = "红色,蓝色,绿色。";
SpannableStringBuilder style = new SpannableStringBuilder(str);
style.setSpan(new ForegroundColorSpan(Color.RED), 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
style.setSpan(new ForegroundColorSpan(Color.GREEN), 4, 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
style.setSpan(new ForegroundColorSpan(Color.BLUE), 6, 9, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(style);
第三种方法,在main.XML中进行设置:
android:textSize="20sp"
android:textColor="#00ff00"
3.设置超链接:
android:autoLink="all" 其中参数可以是:none,email, map ,web, phone, all
4.跑马灯设置:
android:focusable="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusableInTouchMode="true"
android:singleLine="true"