展会信息港展会大全

android Toasts
来源:互联网   发布日期:2015-10-03 11:19:14   浏览:1320次  

导读:1.基础 Context context = getApplicationContext(); CharSequence text = Hello toast!; int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context, text, duration); toast.show(); 当然也可以将方法连起来而不是保持一个Toast对象: Toa...

1.基础

Context context = getApplicationContext();

CharSequence text = "Hello toast!";

int duration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(context, text, duration);

toast.show();

当然也可以将方法连起来而不是保持一个Toast对象:

Toast.makeText(context, text, duration).show();

2.放置位置

toast.setGravity(Gravity.TOP|Gravity.LEFT, x, y);

可以通过setGravity方法来改变toast的默认显示位置

3.自定义Toast

LayoutInflater inflater = getLayoutInflater();

View layout = inflater.inflate(R.layout.custom_toast,

(ViewGroup) findViewById(R.id.toast_layout_root));

TextView text = (TextView) layout.findViewById(R.id.text);

text.setText("This is a custom toast");

Toast toast = new Toast(getApplicationContext());

toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);

toast.setDuration(Toast.LENGTH_LONG);

toast.setView(layout);

toast.show();

赞助本站

人工智能实验室

相关热词: android开发

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港