2016-12-23 221 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()中缺少