這是我的源代碼來創建一個新的ImageView:ImageView:getWidth()與getLayoutParams()不同。
ImageView image = new ImageView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.width = screen_dimens(0.5, "w");
params.height = screen_dimens(0.5, "w");
image.setLayoutParams(params);
(screen_dimens(0.5, "w")
= 50的屏幕寬度的百分比)
但現在,當我想要得到的圖像的寬度,我得到如下結果:
image.getWidth()=> 0
image.getLayoutParams()寬度=> 360
現在是什麼這兩個函數之間的差異?我怎樣才能設置圖像寬度,所以getWidth()
方法也返回正確的值?