imageView返回getWidth零

时间:2015-10-16 19:38:50

标签: android imageview

我在xml中的imageView:

<ImageView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/ivManagePhoto"
                    android:layout_marginTop="10dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp" />

我的代码:

    ImageView ivManagePhoto;
    ivManagePhoto = (ImageView)findViewById(R.id.ivManagePhoto);
   double ivw = ivManagePhoto.getWidth();

当我尝试获取ivManagePhoto.getWidth();返回0,但我需要得到mathparent大小。

2 个答案:

答案 0 :(得分:1)

你可以试试这个:

required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) /* same stuff */ }

或者,试试这个:

imageView.post(new Runnable() {
            @Override
            public void run() {
                imageView.getWidth();
            }
        });

希望此代码能为您提供帮助。

答案 1 :(得分:0)

您可以等到onwindowfocuschanged事件。此时UI项已经加载

@Override
public void onWindowFocusChanged(boolean hasFocus) {

    super.onWindowFocusChanged(hasFocus);
    if (hasFocus) {
        //get sizes you want
        Point screen_size=new Point();
        getWindowManager().getDefaultDisplay().getSize(screen_size);
        int width = screen_size.x;
        int height = screen_size.y;
        //...
    }
    //...
}
相关问题