Android-自定义圆角Toast提示框
来源:互联网 发布:网络直播招聘范文 编辑:程序博客网 时间:2024/06/04 20:12
有时候我们根据需求需要自己指定toast的样式,例如在界面中间显示、圆角、带图片什么的。
今天我给大家带来的就是自定义圆角带图片显示的toast提示框,先来看看最终效果图:图一为圆角带图片显示的,图二为圆角普通显示。均为居中显示
图1: 图2:
先来看看圆角的实现:颜色设为黑色404040,85%透明度B2
<?xml version="1.0" encoding="UTF-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#B2404040" /> <corners android:bottomLeftRadius="8dp" android:bottomRightRadius="8dp" android:topLeftRadius="8dp" android:topRightRadius="8dp" /></shape>
然后使用圆角设置作为RelativeLayout的背景,写一个图片在上,文字在下的布局:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@drawable/progress_hud_bg" android:paddingBottom="35dp" android:paddingLeft="30dp" android:paddingRight="30dp" android:paddingTop="35dp" > <ImageView android:id="@+id/ImageView" android:layout_width="37dp" android:layout_height="37dp" android:layout_centerHorizontal="true" /> <TextView android:id="@+id/message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/ImageView" android:layout_centerHorizontal="true" android:layout_marginTop="5dp" android:textColor="#FFFFFF" android:textSize="18sp" /></RelativeLayout>
自定义toast的实现代码:
package com.example.toasttest;import android.content.Context;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.widget.ImageView;import android.widget.TextView;import android.widget.Toast;public class ToastUtil extends Toast {private static Toast mToast;public ToastUtil(Context context) {super(context);}/** * 自定义Toast样式 * * @description * @param context * @param resId * @param text * @param duration * hrq 2014-7-10下午2:15:36 */public static Toast makeText(Context context, int resId, CharSequence text,int duration) {Toast result = new Toast(context);// 获取LayoutInflater对象LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);// 由layout文件创建一个View对象View layout = inflater.inflate(R.layout.toast_hud, null);// 实例化ImageView和TextView对象ImageView imageView = (ImageView) layout.findViewById(R.id.ImageView);TextView textView = (TextView) layout.findViewById(R.id.message);//这里我为了给大家展示就使用这个方面既能显示无图也能显示带图的toastif (resId == 0) {imageView.setVisibility(View.GONE);} else {imageView.setImageResource(resId);}textView.setText(text);result.setView(layout);result.setGravity(Gravity.CENTER_VERTICAL, 0, 0);result.setDuration(duration);return result;}public static void showToast(Context context, int resId, String content) {mToast = ToastUtil.makeText(context, resId, content, 100);mToast.show();}}
之后我们就能在任意Activity里调用这个自定义的Toast了:
<pre name="code" class="java">package com.example.toasttest;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class MainActivity extends Activity implements OnClickListener {
private ImageView mIv;
private boolean isShow = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mIv = (ImageView) findViewById(R.id.iv);
mIv.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.iv:
if (isShow) {
//在ToastUtil里做了封装,就可以直接使用,不用每次都手动定义显示的时间间隔等等。
ToastUtil.showToast(getApplicationContext(),
R.drawable.ic_launcher, "网络异常,请查看网络");
isShow = false;
} else {
ToastUtil.showToast(getApplicationContext(),
0, "网络异常");
isShow = true;
}
break;
default:
break;
}
}
}
demo下载
0 0
- Android-自定义圆角Toast提示框
- Android之自定义Toast提示框样式
- html 自定义提示框,仿android的Toast功能
- html仿android的Toast功能:自定义淡出提示框
- Android自定义Toast弹出消息提示
- Android消息提示框Toast
- android 信息提示框 Toast
- Android Toast消息提示框
- 自定义Toast提示框 → AppToastMgr
- React Native 自定义实现【Toast】提示框
- 自定义Dialog, Toast提示
- Android自定义注册页面提示,替换Toast功能
- Android 开发之 Toast (弹出提示框)
- Android Toast提示
- android Toast重复提示
- Android中的Toast提示
- Android Toast提示详解
- Android Toast提示封装
- Ubuntu 下安装 JDK
- ORACLE 创建表空间,用户,权限
- hdu 1864 最大报销额 模型为简单的01背包
- 炼数成金---Rcur网络爬虫
- 设计模式--工厂模式
- Android-自定义圆角Toast提示框
- Struts2得到Request和Session
- 机房收费系统总结——代码独立性
- shell脚本中source aaa.sh时提示 source: not found
- poj 2562 Primary Arithmetic
- 2014阿里前端线上笔试题
- String常量池
- STM32 PWM
- Oracle11g服务启动