2014-11-07 3 views
0

나는 간단한 주문형 토스트를 가지고 있는데, 그 범위 내에서 잘 작동하지만 조각에 문제가있다. findviewbyid 메서드는 파편으로 작업하지 않습니다!
어떻게 해결할 수 있습니까?단편으로 된 사용자 토스트

LayoutInflater inflater = getLayoutInflater(); 
    View layouttoast = inflater.inflate(R.layout.customtoast, (ViewGroup)findViewById(R.id.toastcustom)); 
    ((TextView) layouttoast.findViewById(R.id.texttoast)).setText(message); 

layouttoast.findViewById(R.id.imagetoast)).setBackgroundResource(R.drawable.icon); 



    Toast mytoast = new Toast(getBaseContext()); 
    mytoast.setView(layouttoast); 
    mytoast.setDuration(Toast.LENGTH_LONG); 
    mytoast.show(); 

답변

5

당신은 FragmentonCreateView 방법 아래 코드를 삽입 한 다음이 같은 조각 팽창는 것을보기에서 findViewById 메소드를 호출해야합니다

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View parent = inflater.inflate(R.layout.your_fragment_layout, container, false); 

    View layouttoast = inflater.inflate(R.layout.customtoast,(ViewGroup)parent.findViewById(R.id.toastcustom)); 
    ((TextView) layouttoast.findViewById(R.id.texttoast)).setText(message); 

    layouttoast.findViewById(R.id.imagetoast)).setBackgroundResource(R.drawable.icon); 



    Toast mytoast = new Toast(getBaseContext()); 
    mytoast.setView(layouttoast); 
    mytoast.setDuration(Toast.LENGTH_LONG); 
    mytoast.show(); 
    return parent; 
} 
+0

멋진 답변이지만 asyncktask ..가 있고 내 asynctask에 맞춤 토스트를 사용하고 싶습니다.이 기능을 사용하면 도움이 될까요? –

+0

AsyncTask가 조각 안에 있습니까? – rom4ek

+0

return parent..i 후에 asyncktask .. –

0

이에 따라이 하나의 변화 레이아웃과 ID를 시도해보십시오

LayoutInflater inflater1 = getActivity().getLayoutInflater(); 

View layout = inflater1.inflate(R.layout.custom_toast, (ViewGroup) getActivity().findViewById(R.id.custom_toast_layout_id)); 

// set a message 
TextView text = (TextView) layout.findViewById(R.id.text); 
text.setText("Custom Toast"); 

// Toast... 
Toast toast = new Toast(getActivity().getApplicationContext()); 
toast.setGravity(Gravity.CENTER, 0, 0); 
toast.setDuration(Toast.LENGTH_LONG); 
toast.setView(layout); 
toast.show(); 
관련 문제