Android-重新包装Toast,自定义背景
Android-重新包装Toast,自定义背景
2016-4-27
Android L
算是包装了一个自己使用的小工具。
使用Toast的目的是弹一个提示框。先看一下Toast.makeText方法。
Toast.makeText(getApplicationContext(), this, "弹出一个Toast", Toast.LENGTH_SHORT).show();
使用了Android自己的一个layout,然后把传入的text放到layout的TextView中。
public static Toast makeText(Context context, CharSequence text, @Duration int duration) {
Toast result = new Toast(context);
LayoutInflater inflate = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);
tv.setText(text);
result.mNextView = v;
result.mDuration = duration;
return result;
}
我们想自定义个Toast背景,并且调用方法和原来的Toast类似。
观察一下上面的Toast,使用的是Android里面的View。在这一步把View改成我们自己的即可达到目的。
新建文件ToastCustom.java
public class ToastCustom {
public static final int LENGTH_SHORT = Toast.LENGTH_SHORT;
public static final int LENGTH_LONG = Toast.LENGTH_LONG;
Toast toast;
Context mContext;
TextView toastTextField;
public ToastCustom(Context context, Activity activity) {
mContext = context;
toast = new Toast(mContext);
toast.setGravity(Gravity.BOTTOM, 0, 260);// 位置会比原来的Toast偏上一些
View toastRoot = activity.getLayoutInflater().inflate(R.layout.toast_view, null);
toastTextField = (TextView) toastRoot.findViewById(R.id.toast_text);
toast.setView(toastRoot);
}
public void setDuration(int d) {
toast.setDuration(d);
}
public void setText(String t) {
toastTextField.setText(t);
}
public static ToastCustom makeText(Context context, Activity activity, String text, int duration) {
ToastCustom toastCustom = new ToastCustom(context, activity);
toastCustom.setText(text);
toastCustom.setDuration(duration);
return toastCustom;
}
public void show() {
toast.show();
}
}
新建一个layout toast_view.xml,里面的style自己定义
<?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">
<TextView
android:id="@+id/toast_text"
style="@style/TextToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/shape_toast"
android:textColor="@color/white" />
</LinearLayout>
调用方式和Toast一样:
ToastCustom.makeText(getApplicationContext(), this, "弹出自定义背景Toast",
ToastCustom.LENGTH_SHORT).show();
之后就可以很方便地使用这个自定义背景的Toast
Android-重新包装Toast,自定义背景的更多相关文章
- Android特效专辑(三)——自定义不一样的Toast
Android特效专辑(三)--自定义不一样的Toast 大家都知道,Android的控件有时候很难满足我们的需求,所以我们需要自定义View.自定义的方式很多,有继承原生控件也有直接自定义View的 ...
- 安卓Toast自定义及防止重复显示
Toast是安卓系统中,用户误操作时或某功能执行完毕时,对用户的一种提示,它没有焦点,并在一定时间内会消失,但用户连续误操作(如登录时,密码错误)多次时,则会有多个Toast被创建,系统会把这些toa ...
- Android学习笔记_34_自定义窗口标题
1.建好项目之后在它的layout文件夹下创建一个title.xml文件,作为自定义窗口标题的文件. <?xml version="1.0" encoding="u ...
- Android动画效果之自定义ViewGroup添加布局动画
前言: 前面几篇文章介绍了补间动画.逐帧动画.属性动画,大部分都是针对View来实现的动画,那么该如何为了一个ViewGroup添加动画呢?今天结合自定义ViewGroup来学习一下布局动画.本文将通 ...
- 制作自定义背景Button按钮、自定义形状Button的全攻略(转)
在Android开发应用中,默认的Button是由系统渲染和管理大小的.而我们看到的成功的移动应用,都是有着酷炫的外观和使用体验的.因此,我们在开发产品的时候,需要对默认按钮进行美化.在本篇里,笔者结 ...
- listview自定义背景以及item自定义背景
item向自定义背景,可以根据position来设置不同的背景. listview背景设置是需要注意设置下面这几项: //点下时整个页面的背景 android:cacheColorHint=" ...
- 【转】Android 系统菜单与自定义菜单
Android 系统菜单与自定义菜单实现方法如下:系统菜单显示DefaultMenu.java package com.wxz.menu; import com.wxz.menu.R; import ...
- Android 用 camera2 API 自定义相机
前言 笔者因为项目需要自定义相机,所以了解了一下 Android 关于 camera 这块的 API.Android SDK 21(LOLLIPOP) 开始已经弃用了之前的 Camera 类,提供了 ...
- Android特效专辑(五)——自定义圆形头像和仿MIUI卸载动画—粒子爆炸
Android特效专辑(五)--自定义圆形头像和仿MIUI卸载动画-粒子爆炸 好的,各位亲爱的朋友,今天讲的特效还是比较炫的,首先,我们会讲一个自定义圆形的imageView,接着,我们会来实现粒子爆 ...
随机推荐
- 生成JSON数据--fastjson(阿里)方法
fastjson(阿里)方法生成JSON数据: 与Gson类似,创建相应类,再使用JSON.toJSONString()添加对象 要求:生成如下JSON数据 1.{"age":3, ...
- Eclipse中如何显示代码行
方法一 快捷键方式: 按住 Ctrl + F10 选择 show Line Numbers 方法二 手动操作: Window -- Prefences -- General -- Editors - ...
- htm语言的语法基础及规则
HTML的主要语法是元素和标签.元素是符合DTD(文档类型定义)的文档组成部分,如title(文档标题).IMG(图象).table(表格)等等.元素名不区分大小写的.HTML用标签来规定元素的属性和 ...
- 限制容器对内存的使用 - 每天5分钟玩转 Docker 容器技术(27)
一个 docker host 上会运行若干容器,每个容器都需要 CPU.内存和 IO 资源.对于 KVM,VMware 等虚拟化技术,用户可以控制分配多少 CPU.内存资源给每个虚拟机.对于容器,Do ...
- ionic2 跳转子页面隐藏底部导航栏
第一种方法: 在tab里面添加一个属性[tabsHideOnSubPages]='true' <ion-tab [root]="tab1Root" [tabsHideOnSu ...
- python大规模爬取京东
python大规模爬取京东 主要工具 scrapy BeautifulSoup requests 分析步骤 打开京东首页,输入裤子将会看到页面跳转到了这里,这就是我们要分析的起点 我们可以看到这个页面 ...
- jquery判断按钮是否被选中了
<script type="text/javascript"> function genjin_view2(elm){ if($(elm).attr("che ...
- 后端对数组json_encode,前端遍历输出
echo json_encode($get_city_lists); <script type="text/javascript"> function get_city ...
- 如何解释json的字符串
public void getToken(){ String json = getJedis().get("f2b9152f36424e8b8a454df9b50eb743"); ...
- 4.vbs的循环,switch,判断等语句
1.条件判断语句 If Then Else Sub judge(x) Then MsgBox "the num is 0" Then MsgBox "the num is ...