我試圖以編程方式在主要活動上顯示圖像。我之前在ContentFragment上成功完成了這個任務,但由於某種原因,在成功構建之後,圖像不存在。在主要活動中顯示圖像問題
我的示例代碼:
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View myView = inflater.inflate(R.layout.activity_main, null);
ViewGroup mainLayout = (RelativeLayout) myView;
ImageView wView = new ImageView(this);
Drawable W = ContextCompat.getDrawable(this, R.drawable.w);
RelativeLayout.LayoutParams imageParam =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
wView.setImageDrawable(W);
imageParam.setMargins(100, 100, 0, 0);
mainLayout.addView(wView);
wView.setLayoutParams(imageParam);
非常感謝,這個工作正是我怎麼想。另外我喜歡你如何堆疊RelativeLayout初始化。這看起來很整齊。 –