2012-12-30 25 views
6

ben LayoutInflater amacını anlamıyorum çünküandroid özel tost

LayoutInflater inflater = getLayoutInflater(); 

    View layout = inflater.inflate(R.layout.custom_toast_layout, (ViewGroup)findViewById(R.id.custom_toast)); 

    TextView text = (TextView) layout.findViewById(R.id.toast_tv); 
    text.setText("Hello! This is a custom toast!"); 

    Toast toast = new Toast(getApplicationContext());  
    toast.setDuration(Toast.LENGTH_LONG); 
    toast.setView(layout); 
    toast.show(); 

Ancak bu kodu kullanarak özel tost yapmak başardı, bu kod ... modifiye

Toast toast = new Toast(getApplicationContext()); 
    toast.setView(findViewById(R.id.custom_toast)); 
    toast.setDuration(Toast.LENGTH_SHORT); 
    toast.show(); 

Ve RuntimeException "setview denilen olmalı" diyerek olsun ..

  • Neden yapamıyorum LayoutInflater kullanmadan görünümü tost atayın mı?

  • Bu deneyimi diğer özel görünümler için uygulayabilmem için genel olarak LayoutInflater'un amacı nedir?

Düzenleme: Ben onListItemClick() arabirim yönteminde .. içeriği ayarlandıktan sonra bu kodları kullanıyorum ..

+0

ben "içten do LayoutInflater ne" iyi bir soru olacağını düşünüyorum ederiz? – Behnam

cevap

2

Sorunuz cevap, her özel görünümü nedeni bu, ilk şişirmek gerekir var değiştirilmiş kodunuzla ilgili bir hata oluştu. Eğer biz ilk özel görünümü infalte kullanmak zorunda atama özel görünüm için cevap, var

+0

Eğer xml'den java'ya bir Button atarsam, findViewById iyi çalışıyor ... Orada herhangi bir inflater kullanmıyorum .. Neden burada kullanıyorsun? Bu 'setContentView' zaten bir kez kullanılır ve daha fazla içerik değişiklikleri ** düzeni şişirme ** olmalıdır? – BLOB

+0

çünkü tost özeldir ve düğme (android varsayılan) değildir. xml ile düğmenin bazı özelliklerini atayabilir/değiştirebilirsiniz. –

1
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(); 

Bu da onu yaptıklarını olduğunu ve tam doğru bir koddur.

Eğer