我只想以编程方式添加到Linearlayout
简单textView
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = new TextView(this);
tv.setText("Any text");
View parent = findViewById(android.R.id.content);
LinearLayout l = (LinearLayout)parent.findViewById(R.layout.activity_main);
l.addView(tv);
}
然而我收到了错误。
我确信我错过了一些明显的东西
提前谢谢。
答案 0 :(得分:0)
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = new TextView(this);
tv.setText("Any text");
LinearLayout l = (LinearLayout)findViewById(R.id.linearLayoutId);
l.addView(tv);
}
线性布局包含在activity_main.xml文件中