Get Height/Width of imageView from Android.Support

2019-08-12 04:43发布

问题:

I'm using V4.Fragments. So from activity i'm creating new instance of my fragment and after that i begin transaction.
The main problem,that my imageview control is defined(in axml) as Layout_width="match_parent" and weightSum=1 (for height).
For some reason i need to know height/width of my imageView.
What i tried :
was created class that implements IOnGlobalLayoutListener

     class GlobalLayoutListener : Java.Lang.Object, ViewTreeObserver.IOnGlobalLayoutListener
        {
            System.Action _OnGlobalLayout;

            public GlobalLayoutListener (System.Action onGlobalLayout)
            {
                this._OnGlobalLayout = onGlobalLayout;
            }

            public void OnGlobalLayout ()
            {
                _OnGlobalLayout ();
            }
        }

After that,in my V4.Fragment class,i'm doing this:

           public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
                {
                    Arguments = new Bundle();
                    View _View = inflater.Inflate(Resource.Layout.MyFragmentLayout, container, false);
                    imgView = _View.FindViewById<ImageView>(Resource.Id.imgView);

                Action CallBack = () =>
                {
                    ImgHeight = imgView.Height;
                    ImgWidth = imgView.Width;
                };
           //new instance of class,that implements IOnGlobalLayoutListener
            GlobalLayoutListener Global = new GlobalLayoutListener(CallBack);
            imgView.ViewTreeObserver.AddOnGlobalLayoutListener(Global);  
retun _View;
}

Trick doesnt worked. Always return zero(height/width).
Second variant,i implemented interface of IOnGlobalLayoutListener directly in MyFragment

 public class MyCustomFragment : Android.Support.V4.App.Fragment,View.IOnTouchListener,ViewTreeObserver.IOnGlobalLayoutListener 
    {
        int ImgHeight;
        int ImgWidth;
        ImageView imgView;
        Bundle Arguments;

  public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
            {
                Arguments = new Bundle();
                View _View = inflater.Inflate(Resource.Layout.MyFragmentLayout, container, false);
                imgView = _View.FindViewById<ImageView>(Resource.Id.imgView);

        ViewTreeObserver Vto = imgView.ViewTreeObserver; //also tried with _View.ViewTreeObserver; result same
        Vto.AddOnGlobalLayoutListener(this);

    retun _View;
}  

Same problem,doesnt worked.
And my last variant is like :

        public class MyCustomFragment : Android.Support.V4.App.Fragment
            {
                int ImgHeight;
                int ImgWidth;
                ImageView imgView;
                Bundle Arguments;



  public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
            {
                Arguments = new Bundle();
                View _View = inflater.Inflate(Resource.Layout.MyFragmentLayout, container, false);
                imgView = _View.FindViewById<ImageView>(Resource.Id.imgView);

        imgView.ViewTreeObserver.GlobalLayout += (sender, e) =>  //also tried with _View
                {
                    ImgHeight = imgView.Height;
                    ImgWidth = imgView.Width;
                };

    retun _View;
}    

Again no luck,what i'm doing wrong? Thanks.

PS Sorry for my eng.

回答1:

All works fine,i was doing something wrong.
So this code works great :

imgView.ViewTreeObserver.GlobalLayout += (sender, e) => 
                {
                     ImgHeight = imgView.Height;
                     ImgWidth = imgView.Width;
                };  

Enjoy!