将值传递给android中的自定义视图 [英] Passing values to custom view in android

查看:66
本文介绍了将值传递给android中的自定义视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

干杯

我有一个应用程序可以接收用户输入(两个数字,宽度和高度),并且理论上根据该输入,我有一个应该绘制网格(宽度和高度)的自定义视图.

I have an app that receives user input (2 numbers, width and height) and in theory depending on that input I have a custom view that should draw a grid (width and height).

注意:

  • 在视图尝试绘制自身之前,应先接收这2个值.
  • 这两个值不是恒定的,因此我认为XML方法无济于事.
  • 有人告诉我,向View构造函数添加另一个参数是邪恶的.
  • 不要将我的2个值与canvas.getWidth或其他混淆.这些值只是为了绘制东西而已.
  • 我的视图"也是一个ViewGroup.
  • 主要问题出现在XML文件中声明的视图中.

我已经通过制作一个包含两个静态值的SchemeContext类来暂时解决了这个问题,我只是将它们设置在onCreate中(在onCreateView之前),然后在需要时在自定义View onDraw中使用它们(SchemeContext.width).人们并不是真的称呼OOP,我是在Java上强制使用全局变量,并且由于片段的生命周期而按时设置了这些变量.

I have temporarily solved this issue by making an SchemeContext class which contains those 2 static values and I simply set them in onCreate (before onCreateView) then use them in custom View onDraw when needed (SchemeContext.width). This is not really what people would call OOP I'm forcing global variables upon java and those are set on time because of the fragment lifecycle.

我已经看到了这个答案如何在调用onDraw()之前将变量传递给自定义View?.

I've seen this answer How to pass variables to custom View before onDraw() is called?.

但这比解决方案(而不是最快的解决方案)更多是一种解决方法.必须有一个明智的解决方案,我认为android上的3D游戏不会采用这些解决方法(带有OpenGL的SurfaceView仍然是View的权利吗?:d).

But it's more of a workaround than a solution (and probably not the fastest one). There has to be a sensible solution I don't think 3D games on android resort to these workarounds (SurfaceView with OpenGL is still a View right? :d).

如果有一个明显的解决方案,并且这是一个明显的两倍,那么我将删除该问题.

If there is an obvious solution and this is an obvious double I'll remove the question.

推荐答案

我还没有尝试过,但是我认为通过覆盖LayoutInflater.Factory可以很干净地做到这一点.这样,您可以拦截需要将其他参数传递给其构造函数的视图的创建,然后让其余视图陷入默认的膨胀状态.

I haven't tried this, but I think it would be possible to do this fairly cleanly by overriding the LayoutInflater.Factory. That way, you can intercept the creation of the views that need additional parameters passed to their constructors, and let the rest of them fall through to default inflation.

例如,在您的活动中,在增加视图层次结构之前:

For example, in your activity, before you inflate the view hierarchy:

LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
MyInflaterFactory factory = new MyInflaterFactory();
// Pass information needed for custom view inflation to factory.
factory.setCustomValue(42);
inflater.setFactory(factory);

对于工厂的实施:

class MyInflaterFactory implements LayoutInflater.Factory {
    public void setCustomValue(int val) {
        mCustomVal = val;
    }

    @Override
    public View onCreateView (String name, Context context, AttributeSet attrs) {
        if (name.equals("com.package.ViewWithCustomCreation")) {
            return new ViewWithCustomCreation(context, attrs, mCustomVal);
        }
        return null;
    }

    private int mCustomVal;
}

这篇关于将值传递给android中的自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆