ImageView.getWidth() returns 0

前端 未结 4 1152
误落风尘
误落风尘 2020-12-08 21:28

I get imageView\'s width 0. Below is code.

xml file :



        
4条回答
  •  有刺的猬
    2020-12-08 21:34

    Where you calling getWidth() and getHeight() on ImageView? If you calling from onCreate() in activity, it won't work. You need to wait for activity window to attached and then call getWidth() and getHeight() on ImageView. You can try calling getWidth() and getHeight() from onWindowFocusChanged() method of your activity.

    call on onWindowFocusChanged like this way

    public void onWindowFocusChanged(boolean hasFocus) {
        // TODO Auto-generated method stub
        super.onWindowFocusChanged(hasFocus);
         if (hasFocus) {
            ImageView img = (ImageView) findViewById(R.id.img);
            Log.d(TAG, "width : " + img.getWidth());
         }
    
        }
    

提交回复
热议问题