从Android.Support.V4.Fragment获取imageView的高度/宽度(总是返回零) [英] Get Height/Width of imageView from Android.Support.V4.Fragment(always return zero)
问题描述
我正在使用V4.片段.因此,从活动中,我将创建片段的新实例,然后开始交易.
主要问题是,我的imageview控件在axml中定义为Layout_width="match_parent"
和weightSum=1
(用于高度).
由于某些原因,我需要了解imageView的 高度 / 宽度 .
我尝试过的事情:
创建了实现 IOnGlobalLayoutListener
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 ();
}
}
在那之后,在我的V4.Fragment类中,我正在这样做:
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;
}
把戏没有用.始终返回零(高度/宽度).
第二个变体,我直接在 MyFragment
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.
对不起,我的工程师.
推荐答案
一切正常,我做错了事.
因此,这段代码非常有用:
All works fine,i was doing something wrong.
So this code works great :
imgView.ViewTreeObserver.GlobalLayout += (sender, e) =>
{
ImgHeight = imgView.Height;
ImgWidth = imgView.Width;
};
享受!
这篇关于从Android.Support.V4.Fragment获取imageView的高度/宽度(总是返回零)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!