查找当前通过setConteView(...)膨胀的视图

时间:2015-04-21 04:43:59

标签: android android-layout

我只想以编程方式添加到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);
    }

然而我收到了错误。
我确信我错过了一些明显的东西 提前谢谢。

1 个答案:

答案 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文件中