2016-12-23 259 views
-1

我即將設置ImageViewsetImageResource,但它不起作用。沒有錯誤。和logcat似乎很好。setImageResource not working

這裏是我的代碼:

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_item_list_request, container, false); 
    setHasOptionsMenu(true); 

    View anotherView = inflater.inflate(R.layout.style_fragment_list_request,container,false); 

    imgView = (CircleImageView)anotherView.findViewById(R.id.listPhoto); 

    imgView.setImageResource(R.drawable.pastel_red); 
} 

考慮到我打了個電話,從另一個佈局聲明ImageView。如果我膨脹2佈局只是爲另一個佈局的設置圖像是問題?

這裏有什麼問題嗎?

+0

是否在inflat和設置圖像資源之後將其他視圖佈局添加到主佈局? –

+0

不,我先膨脹另一個視圖然後我設置圖像。 查看視圖= inflater.inflate(R.layout.fragment_item_list_request,container,false); setHasOptionsMenu(true); 查看anotherView = inflater.inflate(R.layout.style_fragment_list_request,container,false); imgView =(CircleImageView)anotherView.findViewById(R.id.listPhoto); imgView.setImageResource(R.drawable.pastel_red); } – Borom1r

+0

你必須添加anotherView,否則不會顯示。 –

回答

0
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_item_list_request, container, false); 
    setHasOptionsMenu(true); 
    return view; 
} 

public void onActivityCreated(){ 
    ViewGroup layout = (ViewGroup) getView().findById(R.id.layout_another_view); // need layout for anotherView in fragment_item_list_request.xml 

    View anotherView = inflater.inflate(R.layout.style_fragment_list_request,container,false); 
    imgView = (CircleImageView)anotherView.findViewById(R.id.listPhoto); 
    imgView.setImageResource(R.drawable.pastel_red); 

    layout.addView(anotherView); 
} 
0

如果你這樣做,而不是:

View anotherView = inflater.inflate(R.layout.style_fragment_list_request,null,false); 
+0

仍然無法工作 – Borom1r

0

聲明返回anotherView; onCreateView()中缺少