自定义Toast组件
2016-10-27 11:38
393 查看
自定义Toast组件
本篇博客为原创博客,转载请复制 http://blog.csdn.net/u013298947/article/details/52944397连接哦~:1.自定义Toast布局文件
自定义的Toast布局文件中包括ImageView和TextView,不用多解释:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <RelativeLayout android:id="@+id/toast_lay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="@drawable/feed_toast_bg"> <ImageView android:id="@+id/is_feed_ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" android:src="@drawable/feed_submit_ok"/> <TextView android:id="@+id/feed_submit_txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/is_feed_ok" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:text="@string/feed_is_ok" android:textColor="@color/white" android:textSize="14sp"/> </RelativeLayout> </RelativeLayout>
2.代码中加载布局文件,进行Toast显示设置
代码块语法遵循标准markdown代码,例如:
public void showToast(Context mContext,String str) { LayoutInflater inflater = getLayoutInflater(); View layout= inflater.inflate(R.layout.feed_toast, (ViewGroup) findViewById(R.id.toast_lay)); ImageView image = (ImageView) layout.findViewById(R.id.is_feed_ok); image1.setImageResource(R.drawable.feed_submit_error); TextView feed_submit_txt = (TextView) layout.findViewById(R.id.feed_submit_txt); feed_submit_txt.setText(str+""); Toast toast = new Toast(mContext); //设置Toast显示位置 toast.setGravity(Gravity.CENTER, 0, 0); //设置Toast显示时间 toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); toast.show(); }
3.最后在代码中调用showToast方法即可进行自定义布局显示。
相关文章推荐
- Vue自定义toast组件的实例代码
- vue 自定义提示框(Toast)组件的实现代码
- Android自定义组件系列【17】——教你如何高仿微信录音Toast
- Android自定义组件系列【17】——教你如何高仿微信录音Toast
- 提示框的优化之自定义Toast组件之(二)Toast组件的业务逻辑实现
- 提示框的优化之自定义Toast组件之(三)Toast组件优化
- 提示框的优化之自定义Toast组件之(一)Toast组件的布局实现
- 无刷新省市三级联动自定义组件代码
- 几个经常需要自定义的组件:UIScrollview、UItextView、UIButton
- Axure学习之流程图组件和自定义部件
- Android (界面编程#8-自定义组件)
- 自定义Toast并只显示最后一次
- 构建自定义组件
- 自定义 Toast
- Android 自定义listview 添加监听器 解决屏幕滑动组件状态干扰的问题checkbox
- 【Android基础入门〖17〗】自定义标签 和 自定义组件
- 自定义 React Native 二维码扫描组件
- Oracle ADF table列自定义过滤组件的方法
- Flex 3在ActionScript 中构建自定义组件
- js框架 dwz jui 的日历组件 添加自定义事件