0

我想放入现有的滚动LinearLayout(*1)另一个Layouts(*2). 我有点困惑该走哪条路。

First one:

要在 xml 中定义Layout(*2)并将其从资源加载到应用程序,请设置其子元素(文本/编辑/图标视图)值(unique_id/text),然后将其放入 scrolled Layout(*1)。

Second one:

Layout(*2)在代码中从头开始构建,然后将其插入LinearLayout(*1).

我知道一般规则是在 xml 中定义布局。但这并没有什么不同。如果我们考虑删除这些元素会怎样。我的意思是如果每个插入的布局都被程序作为一个单独的对象可见?

感谢澄清。

编辑:当我遵循第二种方式时,很容易设置值并且设置属性有问题,但它确实有效。只是想知道第一种方式是否有意义。

4

2 回答 2

0

In order to use the first approach, you have to inflate the layout resource using the LayoutInflater, you can get it in your activity calling the method getLayoutInflater(). Then you can add the View you are getting this way by calling

yourView.addView(newView)

EDIT

Both ways are good, it's up to you to choose which one. Generally I would use the first approach, to keep the code cleaner from the set up of attributes.

于 2013-10-20T08:23:23.753 回答
0

尝试使用这个:-

setContentView(ll);

LinearLayout ll = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.common_layout)
TextView tv = new TextView(this);
tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tv.setText("sample text");
ll.addView(tv);

可能会有帮助。

于 2013-10-20T08:28:22.320 回答