简体   繁体   English

如何通过活动类将参数传递给android中的类扩展视图

[英]How to pass parameters to a class extending view in android from activity class

I have a class which extends Activity. 我有一个扩展Activity的类。

I also have a layout for that activity which includes a videoview and progressbar . 我还为该活动提供了一个布局,其中包括一个videoviewprogressbar

There is another class which extends View and creates rectangles on top of the progress bar in the activity layout. 还有另一个类,用于扩展View并在活动布局的进度栏顶部创建矩形。

I want both the main layout and the rectangles created by view class to be visible in the same screen. 我希望视图布局创建的主布局和矩形在同一屏幕上可见。

I need to pass parameters to the view class to set the positions of the rectangle. 我需要将参数传递给视图类以设置矩形的位置。

As far as I know, you cannot set 2 content types from an activity class. 据我所知,您不能从活动类中设置2种内容类型。

So I ended up calling the view class from my layout like this: 因此,我最终从布局中调用了视图类,如下所示:

  <com.example.user.CanvasHeat
    android:id="@+id/viewCanvas"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

But, this way I cannot pass parameters to the CanvasHeat class. 但是,这种方式无法将参数传递给CanvasHeat类。

And if I create the CanvasHeat class from the Activity Class then I cannot set the main layout and the rectangles simultaneously. 而且,如果我从Activity类创建CanvasHeat类,则无法同时设置主布局和矩形。

How should I display both the layout and the rectangles in the same screen and also pass parameters to the view class? 如何在同一屏幕上同时显示布局和矩形,以及如何将参数传递给视图类?

Thanks 谢谢

The only way I see is to declare some methods in your view class to set properties after it was inflated in Activity. 我看到的唯一方法是在Activity中将其夸大之后,在视图类中声明一些方法来设置属性。 Like this: 像这样:

class CanvasHeat extends View {
    public void setValue(int value){ ... }
}

Then in your Activity: 然后在您的活动中:

View viewCanvas = findViewById(R.id.viewCanvas);
viewCanvas.setValue(42);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM