Android 使用现有视图对象膨胀视图

发布于 2024-11-03 05:45:08 字数 752 浏览 0 评论 0原文

我有一个自定义视图,我想从资源模板创建它。我的自定义视图构造函数接受设置为自定义视图的附加信息的附加参数。

问题是,当我膨胀视图时,我得到的视图对象不是自定义视图的子类,因为膨胀方法是静态的,并返回通用的新视图而不是自定义视图的实例。

我正在寻找一种通过传递我的自定义视图对象引用来膨胀视图的方法。

public class MLBalloonOverlayView extends View {
    MiscInfo mMiscInfo;
    public MLBalloonOverlayView(Context context, MiscInfo miscInfo) {
        super(context);
        mMiscInfo = miscInfo;
    }
    public View create(final int resource, final OverlayItem item, 
                        MapView mapView, final int markerID) {
        ViewGroup viewGroup = null;
        View balloon = View.inflate(getContext(), resource, viewGroup);

      // I want to return this object so later I can use its mMiscInfo
      //return this;
        return balloon;
    }
}

I have a custom view that I would like to create from a resource template. My custom view constructor accepts additional parameters that are set as additional info for the custom view.

Problem is when I inflate the view I get a view object that is not subclassed from from custom view since the inflate method is static and returns a generic new view instead of an instance of my custom view.

Would I am looking for is a way to inflate the view by passing it my custom view object reference.

public class MLBalloonOverlayView extends View {
    MiscInfo mMiscInfo;
    public MLBalloonOverlayView(Context context, MiscInfo miscInfo) {
        super(context);
        mMiscInfo = miscInfo;
    }
    public View create(final int resource, final OverlayItem item, 
                        MapView mapView, final int markerID) {
        ViewGroup viewGroup = null;
        View balloon = View.inflate(getContext(), resource, viewGroup);

      // I want to return this object so later I can use its mMiscInfo
      //return this;
        return balloon;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乞讨 2024-11-10 05:45:08

查看代码后 https://github.com/galex/android-mapviewballoons
我能够相应地更新我的代码。这个想法是您从资源创建一个布局,然后将膨胀的视图添加到扩展布局的类的实例中(如上面 Marcos 所建议的那样)。

public class MLBalloonOverlayView extends FrameLayout {

    public MLBalloonOverlayView(Context context, final OverlayItem overlayItem) {
        super(context);
        mOverlayItem = overlayItem;
    }

    public void create(final int resource, MapView mapView, final int markerID) {
        // inflate resource into this object
        TableLayout layout = new TableLayout(getContext());
        LayoutInflater.from(getContext()).inflate(resource, layout);
        TableLayout.LayoutParams params = new TableLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.NO_GRAVITY;
        this.addView(layout, params);
    }
}

After looking at code at https://github.com/galex/android-mapviewballoons
I was able to update my code accordingly. The idea being you create a layout from the resource and then you add the inflated view to the instance of a class that extends a layout (as Marcos suggested above).

public class MLBalloonOverlayView extends FrameLayout {

    public MLBalloonOverlayView(Context context, final OverlayItem overlayItem) {
        super(context);
        mOverlayItem = overlayItem;
    }

    public void create(final int resource, MapView mapView, final int markerID) {
        // inflate resource into this object
        TableLayout layout = new TableLayout(getContext());
        LayoutInflater.from(getContext()).inflate(resource, layout);
        TableLayout.LayoutParams params = new TableLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.NO_GRAVITY;
        this.addView(layout, params);
    }
}
暖风昔人 2024-11-10 05:45:08

将其充气到您的物体上。

public View create(final int resource, final OverlayItem item, 
                    MapView mapView, final int markerID) {
  LayoutInflater.from(getContext()).inflate(resource, this, true);
  return this;
}

Inflate it on your object.

public View create(final int resource, final OverlayItem item, 
                    MapView mapView, final int markerID) {
  LayoutInflater.from(getContext()).inflate(resource, this, true);
  return this;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文