【问题标题】:Get Height/Width of imageView from Android.Support.V4.Fragment(always return zero)从 Android.Support.V4.Fragment 获取 imageView 的高度/宽度(总是返回零)
【发布时间】:2015-10-31 00:19:03
【问题描述】:

我正在使用 V4。片段。所以从活动中我正在创建我的片段的新实例,然后我开始交易。
主要问题是,我的 imageview 控件被定义(在 axml 中)为Layout_width="match_parent"weightSum=1(用于高度)。
出于某种原因,我需要知道我的 imageView 的 height/width
我尝试了什么:
已创建实现 IOnGlobalLayoutListener

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

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

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

在那之后,在我的 V4.Fragment 类中,我正在这样做:

           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;
}

诡计不起作用。始终返回(高度/宽度)。
第二种变体,我直接在MyFragment中实现了IOnGlobalLayoutListener的接口

 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;
}  

同样的问题,没用。
我的最后一个变种是:

        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;
}    

再次没有运气,我做错了什么?谢谢。

PS 对不起我的英语。

【问题讨论】:

    标签: android xamarin xamarin.android android-support-library


    【解决方案1】:

    一切正常,我做错了什么。
    所以这段代码很好用:

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

    享受吧!

    【讨论】:

      猜你喜欢
      • 2014-05-01
      • 1970-01-01
      • 2015-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-16
      相关资源
      最近更新 更多