Toast自定义和一个textview中显示不同颜色

来源:互联网 发布:照片查重软件 编辑:程序博客网 时间:2024/05/23 16:54

在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>"));

一样的效果,喜欢哪种用哪种吧