根據老羅視頻學習的TextView控件。
新建一個android項目,在activity_main.xml中定義一個大的LinearLayout佈局,然後再在這個佈局內定義四個垂直排列的LinearLayout佈局,分別實現四個功能。
1.如何顯示文本(URL,不同字體,顏色,大小)
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_weight="1"
android:id="@+id/textView1"
android:textSize="20sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="textView顯示豐富的文本" />
<TextView
android:layout_weight="1"
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="123456"/>
<TextView
android:layout_weight="1"
android:id="@+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="all"/>
</LinearLayout>
layout_weight是權重,比例;textSize是字體大小;
autoLink屬性可以將電話,郵件,網頁等產生超鏈接的效果,None不匹配任何鏈接,web匹配電話,email匹配郵件,phone匹配電話,map匹配映射網址,all匹配所有鏈接。
text1和text2的text屬性在這裏要不要都沒關係,因爲稍後在java代碼中還要動態設置text。
如下:
textView1 = (TextView)findViewById(R.id.text1);
textView2 = (TextView)findViewById(R.id.text2);
String html = "<font color='red'>I love Android</font><br>";//顏色爲紅色
html+="<font color='#0000ff'><big><i>I love Android</i></big></font><p>";//顏色爲#0000ff,並且是大字體
html+="<big><a href='http://www.baidu.com'>百度</a></big>";//顯示爲"百度"兩個字,點擊後打開http://www.baidu.com
CharSequence charSequence = Html.fromHtml(html);//將字符串進行HTML格式化
textView1.setText(charSequence);
textView1.setMovementMethod(LinkMovementMethod.getInstance());
String text = "我的URL:http://www.baidu.com\n";
text+= "我的email:[email protected]\n";
text+="我的電話:+05715697";
textView2.setText(text);
textView1.setMovementMethod(LinkMovementMethod.getInstance());
效果圖如下:
二.在TextView中顯示錶情圖片和文本。
先把image1,image2,image3,image4,image5圖片拷貝到res.drawable文件夾下。
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_weight="1"
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="textView顯示錶情圖像和文件" />
<TextView
android:layout_weight="1"
android:id="@+id/text3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:text="123456"/>
</LinearLayout>
兩個TextView控件,第一個還是用來提示的普通的textview,第二個textview用來加載表情圖片,在這裏設置背景顏色爲黑色,但是稍後會在程序中動態修改爲白色背景。
text3 = (TextView)findViewById(R.id.text3);
text3.setTextSize(20);//設置字體大小
text3.setBackgroundColor(Color.WHITE);//設置背景顏色爲白色
text3.setTextColor(Color.BLACK);//設置字體顏色爲黑色
String html1="圖像1<img src='image1'/>圖像2<img src='image2'/>圖像3<img src='image3'/><p>";
html1+="圖像4<a href='http://www.baidu.com'><img src='image4'></a>";
html1+="圖像5<img src='image5'/><p>";
//把字符串進行HTML格式化,返回一個字符串
CharSequence ch = Html.fromHtml(html1, new ImageGetter() {
@Override
public Drawable getDrawable(String source) {
// TODO Auto-generated method stub
//獲得系統資源的信息,比如圖片信息
Drawable drawable = getResources().getDrawable(getResourceId(source));
//如果是image3圖片,對它進行壓縮處理
if(source.equals("image3")){
drawable.setBounds(0, 0, drawable.getIntrinsicWidth()/2, drawable.getIntrinsicHeight()/2);
}else {
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
}
//return null;//別忘了返回drawable,剛開始返回了null,導致加載不上圖片
return drawable;
}
}, null);
text3.setText(ch);//顯示返回的字符串
text3.setMovementMethod(LinkMovementMethod.getInstance());
效果圖如下,image3被縮放一半;image4圖片有個超鏈接,鏈接了http://www.baidu.com,所以點擊image4後會彈出百度的主頁。
三.單擊鏈接彈出Activity
兩個textview,點擊後分別彈出Activity
在activity_main.xml文件中佈局如下:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_weight="1"
android:id="@+id/textView3"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="textView顯示Activity" />
<TextView
android:layout_weight="1"
android:id="@+id/text4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="123456"/>
<TextView
android:layout_weight="1"
android:id="@+id/text5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="123456"/>
</LinearLayout>
新建一個activity1.xml和activity2.xml。
<?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" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="這是Activity1" />
</LinearLayout>
<?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" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="這是Activity2" />
</LinearLayout>
新建一個繼承自Activity類的Activity1,加載佈局文件activity1.xml。
package com.example.android_laoluo_textview;
import android.app.Activity;
import android.os.Bundle;
public class Activity1 extends Activity {
public Activity1() {
// TODO Auto-generated constructor stub
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setTitle("這是Activity");
setContentView(R.layout.activity1);
}
}
新建一個繼承自Activity類的Activity2,加載佈局文件activity2.xml。
package com.example.android_laoluo_textview;
import android.app.Activity;
import android.os.Bundle;
public class Activity2 extends Activity {
public Activity2() {
// TODO Auto-generated constructor stub
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setTitle("這是Activity2");
setContentView(R.layout.activity2);
}
}
MAinActivity.java中代碼如下:
text4 = (TextView)findViewById(R.id.text4);
text5 = (TextView)findViewById(R.id.text5);
String text1str = "顯示Activity1";
String text2str = "顯示Activity2";
SpannableString spannableString = new SpannableString(text1str);
SpannableString spannableString2 = new SpannableString(text2str);
//點擊"顯示Activity1"這個字符串的每個地方都可以觸發
spannableString.setSpan(new ClickableSpan() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(MainActivity.this, Activity1.class);
startActivity(intent);//這個不要忘記寫,剛開始忘記寫,點擊沒反應
}
}, 0, text1str.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString2.setSpan(new ClickableSpan() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(MainActivity.this, Activity2.class);
startActivity(intent);
}
}, 0,text2str.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
text4.setText(spannableString);
text5.setText(spannableString2);
text4.setMovementMethod(LinkMovementMethod.getInstance());
text5.setMovementMethod(LinkMovementMethod.getInstance());
最後不要忘記在Androidmenifest.xml文件中聲明新添加的兩個Activity類
<activity
android:name="com.example.android_laoluo_textview.Activity1"></activity>
<activity
android:name="com.example.android_laoluo_textview.Activity2"></activity>
點擊"顯示Activity1"會彈出activity1.xml界面,點擊“顯示Activity2”會彈出activity2.xml界面。
四.用textview實現跑馬燈效果。
佈局文件如下:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_weight="1"
android:id="@+id/textView4"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="textView顯示跑馬燈效果" />
<TextView
android:layout_weight="1"
android:id="@+id/text6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"//單行
android:ellipsize="marquee"//文字溢出部分隱藏
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:background="#ffffff"
android:focusableInTouchMode="true"//這個一定要有,不然不會有跑馬燈效果
android:textSize="20sp"
android:autoLink="web"
android:text="123456"/>
</LinearLayout>
android:singleLine="true" 單行顯示
android:ellipsize="marquee" 文字溢出設置。start,省略號在開頭;end,省略號在結尾;middle,省略號在中間;marquee循環;
android:marqueeRepeatLimit="marquee_forever" 循環的次數,無數次循環。
android:focusable="true" 得到焦點後觸發事件
android:focusableInTouchMode="true" 不用獲取焦點,就觸發事件,即一運行就觸發事件。
跑馬燈效果基本就是由這幾個屬性控制的。
MAinActivity.java代碼如下:
text6 = (TextView)findViewById(R.id.text6);
String htmlString = "洪湖是湖北省最大的湖泊,是 <a href='http://www.baidu.com'>中國第七大淡水湖</a>,曾以紅色革命根據地和富饒的“魚米之鄉”而聞名全國";//此處的<a href='http://www.baidu.com'></a>是爲了實現使“中國第七大淡水湖”這幾個字的超鏈接
CharSequence charSequence2 = Html.fromHtml(htmlString);//使字符串HTML格式化
text6.setText(charSequence2);//此處必須使用HTML格式化後的字符串,否則超鏈接<a href=''></a>會以普通字符串的形式出現
text6.setMovementMethod(LinkMovementMethod.getInstance());
效果圖如下:
總體效果圖如下: