Service类中怎么在覆写的onStart方法中调用Toast,要求Toast能实现显示图片和文字
Service类中如何在覆写的onStart方法中调用Toast,要求Toast能实现显示图片和文字
在继承的Activity类中调用Toast,并且自定义Toast,要求其能实现显示图片和文字。配合XML文件(包含ImageView 和TextView),我能做到上面的要求。比如以下代码:
1、xml文件
2、java代码:
但是,如何实现以下要求呢?
在继承的Service类中如何在覆写的onStart(Intent intent, int startId)方法中调用Toast,并且自定义Toast,要求其能实现显示图片和文字。
在继承的Activity类中调用Toast,并且自定义Toast,要求其能实现显示图片和文字。配合XML文件(包含ImageView 和TextView),我能做到上面的要求。比如以下代码:
1、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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#DAAA"
android:orientation="horizontal"
android:padding="10dp" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF" />
</LinearLayout>
2、java代码:
// 获取LayoutInflater对象,该对象能把XML文件转换为与之一直的View对象
LayoutInflater inflater = getLayoutInflater();
// 根据指定的布局文件创建一个具有层级关系的View对象
// 第二个参数为View对象的根节点,即LinearLayout的ID
View layout = inflater.inflate(R.layout.toast_layout,
(ViewGroup) findViewById(R.id.toast_layout_root));
// 查找ImageView控件
// 注意是在layout中查找
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.head);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("这是一个自定义Toast,并且带图片显示");
Toast toast = new Toast(getApplicationContext());
// 设置Toast的位置
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
// 让Toast显示为我们自定义的样子
toast.setView(layout);
toast.show();
但是,如何实现以下要求呢?
在继承的Service类中如何在覆写的onStart(Intent intent, int startId)方法中调用Toast,并且自定义Toast,要求其能实现显示图片和文字。
Android
Service
Toast
- 1eclipse中怎么修改项目名称 具体操作
- 2android 语言定做
- 3Android学习笔记19:含有通知栏的进度条的Android下载文件
- 4(原)Android理论梳理-No1异步处理之Handler相干机制
- 5android GPS 获取定位坐标信息解决方法
- 6FBReaderJ学习笔记(3):Footer底部状态栏更改
- 7ScrollView里嵌套ListView,怎么禁用ListView的滚动事件,或者触发ListView滚动时调用父元素ScrollView的滚动事件
- 8Android快速开发必不可少的11个工具类
- 9listview 动态添加,该如何解决
- 10Android带明白按钮的EditText
文章评论
相关解决方案