在Android項目開發中,經常會用到Toast這個控件,但是系統的默認式樣太難看,有時需要改變一下,比如背景圖片,還有上面的提示文字,有的時候還需要動態改變提示的文字,比如顏色之類的
還有一個問題,在一個TextView上面,怎樣讓它顯示的內容有不同的顏色,比如“今天天氣好嗎?挺好的”,如果想讓“今天今天好嗎?”這一句顯示紅色,“挺好的”這三個字顯示綠色呢?
下面就兩個問題一並做解答,有圖有真相
接下來看代碼:
先是主布局文件main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello"/>
<Button android:text="啟動Toast" android:id="@+id/button1"
android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
</LinearLayout>
相應的Activity
package com.toast;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MyToastActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new MyOnclickListener());
}
private class MyOnclickListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
LayoutInflater inflater = LayoutInflater.from(MyToastActivity.this);
View view = inflater.inflate(R.layout.my_toast, (ViewGroup)findViewById(R.id.toast_layout_root));
TextView textView = (TextView) view.findViewById(R.id.text);
SpannableString ss = new SpannableString("今天天氣好嗎?挺好的");
ss.setSpan(new ForegroundColorSpan(Color.RED), 0, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new ForegroundColorSpan(Color.GREEN), 7, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(ss);
Toast toast = new Toast(MyToastActivity.this);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(view);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}
}
這里注意紅色加粗的部分,就是處理在一個TextView中顯示的文字有不同的顏色的,當然還可以有刪除線,下划線這些。。。
接下來就是Toast所用到的布局文件了my_toast.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:background="@drawable/pop_select_tost"
>
<TextView android:id="@+id/text"
android:layout_width="200dip"
android:layout_height="fill_parent"
android:layout_marginTop="10dip"
android:textColor="@color/black"
android:layout_marginLeft="10dip"/>
</LinearLayout>
為了便於大家測試,一並把所用到的圖片也是附上吧
OK,到此
另外,如果不用SpannableString還可以用Html也可以
textView.setText(Html.fromHtml("<font size=\"3\" color=\"red\">今天天氣好嗎?</font><font size=\"3\" color=\"green\">挺好的</font>"));
一樣的效果,喜歡哪種用哪種吧