【发布时间】: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