Android自定义Toast宽度无法设置问题解决
2015-10-19 01:06
1376 查看
在项目中想要实现一个头部的toast提示效果,类似下图
![](https://oscdn.geek-share.com/Uploads/Images/Content/202002/02/caeaef6d5a9102fc8b05d4e9855a77c6.png)
再实现的过程中发现,如果直接通过修改Toast的View布局的父控件宽度是无法实现效果的,后来是通过直接用代码指定父控件内部的textview的宽度实现了。下面是具体的代码:
上面的方式使用到了我自己写的一个计算屏幕宽高的小方法:
通过上面的步骤就能在标题栏下方显示一个自定义的toast提示
![](https://oscdn.geek-share.com/Uploads/Images/Content/202002/02/caeaef6d5a9102fc8b05d4e9855a77c6.png)
再实现的过程中发现,如果直接通过修改Toast的View布局的父控件宽度是无法实现效果的,后来是通过直接用代码指定父控件内部的textview的宽度实现了。下面是具体的代码:
private static void makeToast(String msg) { if (toastView == null) { LayoutInflater inflater = (LayoutInflater) AppApplication.getInstance().getSystemService(Context.LAYOUT_INFLATER_SERVICE); params = new LinearLayout.LayoutParams(AppApplication.mScreenWidth, ViewGroup.LayoutParams.MATCH_PARENT); toastView = inflater.inflate(R.layout.toast_custom_prompt, null); } TextView tv = (TextView) toastView.findViewById(R.id.tvTitleToast); tv.setLayoutParams(params); tv.setText(msg); toast = new Toast(AppApplication.getInstance()); float hOffset = AppApplication.getInstance().getResources().getDimension(R.dimen.common_title_height); toast.setGravity(Gravity.TOP, 0, (int) hOffset); toast.setDuration(Toast.LENGTH_LONG); toast.setView(toastView); toast.show(); }
上面的方式使用到了我自己写的一个计算屏幕宽高的小方法:
private void calcScreenSize() { DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); mScreenWidth = dm.widthPixels; mScreenHeight = dm.heightPixels; AppApplication.mScreenWidth = mScreenWidth; AppApplication.mScreenHeight = mScreenHeight; }
通过上面的步骤就能在标题栏下方显示一个自定义的toast提示
相关文章推荐
- android 自定义Toast显示风格
- RecyclerView设置Item点击事件
- android 48 广播
- android之Dialog相关
- Android提示版本更新
- android 之杂七杂八
- Cordova 5.3.3(PhoneGap)Android 开发环境搭建
- Android Dialog触摸对话框外部让其消失的实现方法
- ANDROID开发之SQLite详解
- Android事件分发机制完全解析
- android中layout_gravity 和 gravity的区别
- 搜索----Android Demo
- 搜索----Android Demo
- Android中的动画
- Android Studio Git使用
- Android-Activity的四状态、七生命周期、和四启动模式
- Android TextView 使用替换构建出不同样式的字符串
- ubuntu android 设备
- Android_03--四大组件_Activity+BroadcastReceiver+service+ContentProvider
- Android_01--Android入门+四大布局+点击事件+文件操作