Android 自定义Toast
来源:互联网 发布:树莓派3linux armv7l 编辑:程序博客网 时间:2024/06/05 21:49
package com.belter.phonedoctor.widget;import android.app.Activity;import android.content.Context;import android.util.DisplayMetrics;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.WindowManager;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;import android.widget.Toast;import com.belter.myapplication.R;/** * Created by xiaquan on 2017/5/25. */public class CustomToast { private static TextView mTextView; private static ImageView mImageView; public static void showToast(Context context, String message,int color) { //加载Toast布局 View toastRoot = LayoutInflater.from(context).inflate(R.layout.custom_toast, null); //初始化布局控件 mTextView = (TextView) toastRoot.findViewById(R.id.message); //mImageView = (ImageView) toastRoot.findViewById(R.id.imageView); //为控件设置属性 mTextView.setText(message); //mImageView.setImageResource(R.mipmap.ic_launcher); //Toast的初始化 Toast toastStart = new Toast(context); //获取屏幕高度 DisplayMetrics dm = new DisplayMetrics(); ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(dm); int mScreenWidth = dm.widthPixels; int mScreenHeight = dm.heightPixels; WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); int height = wm.getDefaultDisplay().getHeight(); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(mScreenWidth, 100); mTextView.setLayoutParams(params); mTextView.setBackgroundColor(context.getResources().getColor(color)); //Toast的Y坐标是屏幕高度的1/3,不会出现不适配的问题 toastStart.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0); toastStart.setDuration(Toast.LENGTH_LONG); toastStart.setView(toastRoot); toastStart.show(); }}
------------------------------------------------------------------------------
<?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="@dimen/dp_100" android:background="@color/verifycode_error" android:orientation="horizontal"> <TextView android:id="@+id/message" android:gravity="center" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:shadowColor="#bbfcd603" android:shadowRadius="2.75" android:textSize="@dimen/sp_46" android:textColor="@color/white" /></LinearLayout>
阅读全文
0 0
- android自定义Toast视图
- Android中自定义Toast
- Android自定义Toast样式
- android Toast 自定义样式
- 自定义Toast - Android代码
- android中自定义toast
- Android 自定义Toast
- android自定义Toast视图
- android 自定义toast
- 自定义Android Toast
- android自定义toast
- Android Toast自定义
- android自定义toast
- Android之自定义Toast
- Android 自定义Toast实现
- android 自定义toast
- Android Toast自定义
- android 自定义toast
- Caffe 学习:Eltwise层
- JavaScript初学者必看“箭头函数”
- 实现浏览器兼容版的window.getComputedStyle
- 数据库面试题
- Android studio 多渠道(多环境)打包grade配置详解
- Android 自定义Toast
- 一道有意思的题目Intersection of Two Linked Lists(双指针)
- SQL Server系统表sysobjects
- iOS 从0到1搭建高可用App框架
- mybatis注解方式返回insert的id
- springmvc重定向写法及注意
- json动态过滤属性 @jsonignore及@JsonIgnoreProperties
- jar的加载
- linux内核堆栈保护浅析