1

我有一个自定义视图(扩展视图),我想在OnCreate函数中添加控件(按钮/文本框等),以在运行时将这些组件添加到视图中:将表单组件添加到自定义android视图

public Section(Context context) { 
    super(context); 

    this.setBackgroundColor(Color.argb(255, 242, 242, 242)); 
    this.setOnTouchListener(mSectionOnTouch); 

    LinearLayout l = new LinearLayout(this.getContext()); 
    l.setOrientation(LinearLayout.VERTICAL); 
    Button btn = new Button(this.getContext()); 
    btn.setId(1); 
    btn.setText("btn1"); 
    l.addView(btn); 

    Button btn2 = new Button(this.getContext()); 
    btn2.setId(2); 
    btn2.setText("btn2"); 
    l.addView(btn2); 

} // Section 

但这似乎没有做任何事情......有人能告诉我什么即时做错了吗?

非常感谢

FR

回答

0

你从不添加l到您的视图。它应该是这样的:

public Section(Context context) 
{ 
    // setup linear layout 

    addView(l); 
} 

一个稍微简单的方式做这将是具有自定义视图扩展的LinearLayout。然后,您可以直接将视图添加到自定义视图中,而不必嵌套另一个容器,这对性能更好。

+0

扩展LinearLayout而不是查看它 - 干杯:D – FiniteRed

相关问题