Android中自定义Toast视图和修改显示位置
来源:互联网 发布:php过滤敏感词的例子 编辑:程序博客网 时间:2024/06/06 06:41
工作笔记:项目过程中,可能会遇到需要自定义toast的视图和显示位置的需求,这里做个笔记以便日后好使用,需要的话可以直接套用;
按钮点击触发toast提示代码:
//抽取的方式可以建立一个私有的方法,把视图view 和toast文字,显示时长以及taost位置这些都提取到参数列表中
findViewById(R.id.show_toast).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast toast=new Toast(MainActivity.this); toast.setView(View.inflate(MainActivity.this,R.layout.toast,null)); ((TextView)toast.getView().findViewById(R.id.tv_toast)).setText("不可重复点赞"); toast.setGravity(Gravity.CENTER,0,0); toast.setDuration(Toast.LENGTH_SHORT); toast.show();// showToast(View.inflate(MainActivity.this,R.layout.toast,null),"不可重复点赞"); } }); private void showToast(View view,String toastMessage) { Toast toast=new Toast(MainActivity.this); toast.setView(view); //这个id各位根据自己布局内的id填写 ((TextView)view.findViewById(R.id.tv_toast)).setText(toastMessage); toast.setGravity(Gravity.CENTER,0,0); toast.setDuration(Toast.LENGTH_SHORT); toast.show(); }
toast的视图布局代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv_toast" android:layout_centerInParent="true" android:background="@drawable/toast_bg" android:textColor="@android:color/white" /></RelativeLayout>
background背景选择器代码:
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <solid android:color="@android:color/darker_gray" /> <corners android:radius="5dp" /> <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp"/></shape>
阅读全文
0 0
- Android中自定义Toast视图和修改显示位置
- Android中修改toast的显示位置
- Android 自定义Toast,修改Toast样式和显示时长
- Android 自定义Toast,修改Toast样式和显示时长
- android Toast工具类,可自定义位置和取消显示
- android Toast 自定义显示位置 custom position
- 安卓中修改Toast显示位置和内容
- 自定义toast样式和指定显示位置
- Android 中toast显示在不同位置
- Android自定义Toast视图和动画
- android自定义Toast视图
- android自定义Toast视图
- Android自定义Toast,指定Toast信息显示的位置并使用Toast显示其他View
- Android中Toast显示时间的自定义
- Android中Toast显示时间的自定义
- Android中Toast显示时间的自定义
- Android中Toast显示时间的自定义
- android中Toast显示时间自定义使用
- ActiveMq消息队列
- 如何使用Docker、Docker-Compose和Rancher搭建部署Pipeline(三)
- 除了选对主机,网站内容和用户体验更为重要
- EJB到底是什么
- git cherry-pick使用小结
- Android中自定义Toast视图和修改显示位置
- Android基础之最新正则表达式
- log4j+slf4j+HandlerExceptionResolver 日志系统
- Java实现对图的广度优先搜索
- 安卓学习Blog整理
- js和php方式,科学计数法转换成普通数
- ubuntu ceph 块 命令操作
- 输出字符奇数位置的字符串
- 高斯函数以及在图像处理中的应用总结