Android 更改 Toast 的默认位置及自定义Toast
16lz
2021-01-23
先看看两篇文章
Android 更改 Toast 的默认位置
android Toast大全(五种情形)建立属于你自己的Toast
Toast Notifications的API
默认效果的代码
Toast.makeText(MainActivity.this,"默认样式",Toast.LENGTH_SHORT).show();
自定义显示位置效果
private Toast toast; toast= Toast.makeText(MainActivity.this, pickers.getShowConetnt(), Toast.LENGTH_SHORT); toast.setGravity(Gravity.TOP, 0, 0); toast.show();//setGravity参数一个Gravity常量,一个x方向偏移值和一个y方向偏移值。//如果你想要向右移动,增加第二个参数的值;增加第三个参数的值向下移动。
带图片的Toast
private Toast toast; toast= Toast.makeText(MainActivity.this, pickers.getShowConetnt(), Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); LinearLayout toastView = (LinearLayout) toast.getView(); ImageView imageCodeProject = new ImageView(getApplicationContext()); imageCodeProject.setImageResource(R.drawable.icon); toastView.addView(imageCodeProject, 0); toast.show();
完全自定义效果
LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.custom, (ViewGroup) findViewById(R.id.llToast)); ImageView image = (ImageView) layout .findViewById(R.id.tvImageToast); image.setImageResource(R.drawable.icon); TextView title = (TextView) layout.findViewById(R.id.tvTitleToast); title.setText("Attention"); TextView text = (TextView) layout.findViewById(R.id.tvTextToast); text.setText("完全自定义Toast"); toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); toast.show();
完.
更多相关文章
- --------------------【Android】跑马灯效果(文字滚动)
- Activity 属性设置参数说明
- Android:实现一种浮动选择菜单的效果
- Android中ImageButton自定义按钮的按下效果的代码实现方法,附网上
- Android使用okhttp框架实现带参数Get和Post请求(附服务端完整代码
- android 图片切换,仿百叶窗效果