【问题标题】:How can I get a View from a Layout?如何从布局中获取视图?
【发布时间】:2014-02-18 06:55:25
【问题描述】:

我正在使用Xamarin,并且有一个关于从Layout 获取View 的问题。

如果我有一个名为custom_info_windowLayout,我如何从中获得View

我试过这段代码:

View v = Inflate(Resource.Layout.custom_info_window, null);

这是我得到的错误:

错误 CS0103:当前上下文中不存在名称“Inflate”

我可以帮忙吗?

提前致谢

编辑

这是我的充气机代码:

LayoutInflater inflater =(LayoutInflater)Context.GetSystemService(Context.LayoutInflaterService);

这是我得到的错误:

Error CS0120: An object reference is required for the non-static field, method, or property 'Android.Content.Context.GetSystemService(string)'

【问题讨论】:

标签: android layout view xamarin layout-inflater


【解决方案1】:

你应该使用这个..

 LayoutInflater inflater = (LayoutInflater)this.GetSystemService(Context.LayoutInflaterService);
 var layout = inflater.Inflate(Resource.Layout.custom_info_window, layoutImages) as LinearLayout;

【讨论】:

  • 你有自定义视图吗??
  • 是的,我已经制作了自定义视图。
【解决方案2】:
LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = mInflater.inflate((Resource.Layout.custom_info_window, null);

【讨论】:

    【解决方案3】:

    也应该可以从上下文中获取 LayoutInflater,例如:

     LayoutInflater.from(getApplicationContext()).inflate(Resource.Layout.custom_info_window, null);
    

    【讨论】:

    • 这似乎是最简单的方法。
    【解决方案4】:

    您将需要使用 Context 对象来获取服务。如果没有其他方法,请使用静态属性 Application.Context:

    var inflater = Application.Context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;
    var view = inflater.Inflate(...
    

    【讨论】:

      猜你喜欢
      • 2013-07-26
      • 2012-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多