Android Toast自定义
2013-09-29 14:38
183 查看
在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>"));
一样的效果,喜欢哪种用哪种吧
相关文章推荐
- Android Toast 设置到屏幕中间以及其他自定义Toast的实现方法
- Android自定义Toast的时长、位置、及显示的View
- android自定义Toast视图
- Android Toast自定义
- 关于Android 自定义Toast显示风格
- Android 自定义Toast
- Android自定义Toast的使用
- Android 自定义Toast
- Android自定义Toast
- Android特效专辑(三)——自定义不一样的Toast
- Android-自定义Toast
- Android 更改 Toast 的默认位置及自定义Toast
- android 自定义Toast
- Android进阶篇-Toast自定义显示时间
- Android中实现带声音提示的Toast (自定义扩展Toast)
- Android通过Window获取View&模拟点击&自定义Toast
- Android Toast 默认和自定义使用
- android中自定义Toast方法详解(一)
- (转)android Toast 自定义消息显示
- html 自定义提示框,仿android的Toast功能