我要动态地添加一些看法到的LinearLayout
是在XML中已经定义。
I want to dynamically add some views to a LinearLayout
that is already defined in XML.
我可以添加视图屏幕,但他们没有被放在内部的权利的LinearLayout
。
I'm able to add views to the screen but they are not being placed 'inside' the right LinearLayout
.
我怎样才能得到一个引用从code,同样得到一个查看
通过使用 findViewById()?
How can I get a reference to this specific Layout from code, similar to getting a View
by using findViewById()
?
由于Marcarse指出,你可以做
As Marcarse pointed out you can do
ViewGroup layout = (ViewGroup) findViewById(R.id.your_layout_id);
TextView tv = new TextView(this);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setText("Added tv");
layout.addView(tv);
本的LinearLayout类扩展的ViewGroup 本身扩展了View类。这使得获取引用布局得到一个引用到另一个视图一样简单。
The LinearLayout class extends ViewGroup which itself extends the View class. This makes acquiring a reference to a layout as easy as getting a reference to another View.