android TextView如何显示html元素控件
2014-03-15 23:14 kingshow 阅读(1132) 评论(0) 编辑 收藏 举报
在android中,用textview显示丰富的文本,也可以嵌入html元素控件。
一、MainActivity类的代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.study.android_textview; import android.os.Bundle; import android.app.Activity; import android.text.Html; import android.text.method.LinkMovementMethod; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { private TextView textView1,textView2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView1 = (TextView)this.findViewById(R.id.textview1); textView2 = (TextView)this.findViewById(R.id.textview2); //加添一段html String html = "<font color='red'>I love android</font><br>"; html += "<font color='#0000ff'><big><i>I love android</i></big></font><p>"; html += "<big><a href='http://www.baidu.com'>百度</a></big>"; CharSequence charSequence = Html.fromHtml(html); textView1.setText(charSequence); textView1.setMovementMethod(LinkMovementMethod.getInstance());//点击的时候产生超链接 String text = "我的URL:http:www.sina.com\n"; text += "我的Email:abcd@126.com\n"; text += "我的电话:+86 010-89487369"; textView2.setText(text); textView2.setMovementMethod(LinkMovementMethod.getInstance()); } @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; } }
二、strings.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">如何显示html的元素控件</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <color name="green">#00ff00</color> <string name="link_text"><a href="tel:18651032915">打电话</a> </string> </resources>
三、main.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/textview1" android:padding="20sp" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/textview2" android:padding="20sp" android:autoLink="all" android:textSize="20sp" android:text="@string/link_text" /> </LinearLayout>
四、显示效果
未完……