在android中自定义吐司
问题描述:
我想在我的应用程序中使用xzx更改背景和文本颜色xzxx 在android中自定义吐司
LayoutInflater infator = getLayoutInflater();
View layout =infator.inflate(R.layout.toast, (ViewGroup) findViewById(R.id.toast_layout));
TextView toastText = (TextView) findViewById(R.id.toasttext);
toastText.setBackgroundColor(Color.YELLOW);
toastText.setText("uei:"+o.getUei());
Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
此代码存在于我的listview的OnItemClickListener中。
但应用程序因空指针异常而被强制关闭!!
这就是onclick监听器,它可以提供更好的画面。
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alarms);
m_alarmAdapter = new AlarmAdapter(this, R.layout.severity_item, m_alarms);
setListAdapter(m_alarmAdapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(final AdapterView<?> parent, final View view, final int position, final long id) {
Log.d(TAG, "clicked: " + view);
final Alarm o = m_alarmAdapter.getItem(position);
LayoutInflater infator = getLayoutInflater();
答
您正在询问当前视图以查找toasttext
id,而不是您刚刚加载的视图。尝试layout.findViewById()
答
TastyToast Library是一个很好的解决方案,可以在Android中自定义Toast。
您可以使用
TastyToast.makeText(getApplicationContext(), "Hello World !", TastyToast.LENGTH_LONG, TastyToast.WARNING);
TastyToast通过表征吐司的类型分为五大类别,它是成功的,危险,警告,信息和默认帮助用户。
- 对于每个默认类别,它不仅改变背景和文字 颜色,而且在Toast左侧添加可口的自定义视图。
该库提供了灵活性来自定义文本颜色,Toast背景以及每个类别的特定烤面包图标。
LogCat中的简短错误报告08-25 13:25:14.672:ERROR/AndroidRuntime(2879):java.lang.NullPointerException 08-25 13:25:14.672:ERROR/AndroidRuntime(2879):at com.opennms .android.AlarmActivity $ 2.onItemClick(AlarmActivity.java:65) – TechnocraT
哪一行是65行? – ZeissS
你在'findViewById'之前调用过'setContentView'吗? – MByD