基础:

TextView是无法供编辑的。
当我们新建一个项目MyTextView时候,默认的布局(/res/layout/activity_main.xml)中已经有了一个TextView:
<TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />



运行效果如下:


修改其文本内容:


在布局文件activity_main.xml中可以看到:
android:text="@string/hello_world"



hello_world实际上是一个字符串资源,看成变量名就行了。在/res/values/strings.xml下可以找到:
<string name="hello_world">Hello world!</string>



根据需要,修改标签中的内容即可。

为文本内容增加html形式的样式:


可以在字符串资源中添加一定的样式,例如<i>,<b>,<u>。例如让Hello成为斜体:
<string name="hello_world"><i>Hello</i> world!</string>
运行结果如下:



使用Html.fromHtml为TextView中的文本提供样式:


为布局文件activity_main.xml中的TextView添加id:

<TextView        android:id="@+id/myTextView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />



将/src/com.example.mytextview/MainActivity.java内容更改如下:
package com.example.mytextview;import android.os.Bundle;import android.app.Activity;import android.text.Html;import android.text.Spanned;import android.view.Menu;import android.widget.TextView;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);TextView myTextView= (TextView) findViewById(R.id.myTextView);Spanned sp = Html.fromHtml("<h3><u>Hello world!</u></h3>");myTextView.setText(sp);}@Overridepublic 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;}}



效果如下:


使用TextView自带的方法更改其文本内容的样式:


例如MainActivity.java:
package com.example.mytextview;import android.os.Bundle;import android.app.Activity;import android.graphics.Color; //import android.graphics.Paint; //import android.view.Menu;import android.widget.TextView;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);TextView myTextView= (TextView) findViewById(R.id.myTextView);myTextView.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);myTextView.setTextSize(20);myTextView.setTextColor(Color.BLUE);}@Overridepublic 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;}}



效果如下:



如果要在文本中现实<,>等特殊字符,请使用实体引用,例如&lt;等等。

更多相关文章

  1. Android自定义SeekBar样式,遇到的进度条高度问题
  2. LinearLayout不能显示全部内容
  3. Android(安卓)View的滚动scroll 、android.widget.Scroller和 属
  4. 解决android sdk docs帮助文档打开慢的问题
  5. android多线程读取网页内容
  6. android获取单个通讯录联系人信息并分享内容(短信邀请)
  7. Android(安卓)分页控件制成底部菜单
  8. Android(安卓)ListView存在多个item样式的处理方法
  9. Android(安卓)SharedPreferences的使用.

随机推荐

  1. Android(安卓)中Fragment使用
  2. Android:本地json文件解析
  3. Android(安卓)Studio编译从eclipse导入的
  4. android 网络图片查看器
  5. 用代码创建Android视图
  6. Android(安卓)development Notes-3(Activ
  7. Android(安卓)读取SIM卡参数
  8. 移植dlib到android
  9. Android(安卓)Activity设置全屏
  10. 移植dlib到android