在运行时加载CustomView及其逻辑

时间:2013-02-01 09:35:39

标签: android android-custom-view custom-view

我正在为Android App创建一个自定义组件(恰好是音频混音器的音量推子)。它工作正常。

我想要的是在运行时加载此组件的多个实例。 我试过这个:

LinearLayout layout = (LinearLayout)findViewById(R.id.mixerContainer) //This layout
for(int i=0;i<8;i++){
  LayoutInflater l = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  View v = l.inflate(R.Layout.MyVolumeFader, null);
  layout.add(v);
}

这会正确添加视图,但不会附加我的组件的所有逻辑(包含在MyVolumeFader类中)。

我该如何解决这个问题?

-

代码如下:

MyVolumeFader.java

public class MyVolumeFader extends RelativeLayout{
 [...]

 private VerticalSeekBar volumeBar;

 [...]

 ->volumeBar handling<-
}

MyVolumeFader.xml

<RelativeLayout...
Some TextViews, Buttons and a VerticalSeekBar are here...

谢谢。

1 个答案:

答案 0 :(得分:0)

你可以考虑使用windowmanager及其'addview方法来解决你的问题。

mWindowManager.addView(new ChartTools(this, a, list, "业务统计").getChart(),
        ParamTools.getChartParams());