-1

我正在尝试以编程方式将线性布局垂直放置在线性布局内,但它似乎不起作用,按钮没有出现,但文本视图出现......

这是我的代码:(这是一个对话框..)

LinearLayout titleLayout = new LinearLayout(m_context);
titleLayout.setOrientation(LinearLayout.VERTICAL);

m_titleView = new TextView(m_context);
m_titleView.setText(title);


LinearLayout horizontalLayout = new LinearLayout(m_context);
titleLayout.setOrientation(LinearLayout.HORIZONTAL);

Button backward = new Button(m_context);
backward.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
backward.setText("Backwards");

Button newDirButton = new Button(m_context);
newDirButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
newDirButton.setText("New folder");

horizontalLayout.addView(backward);
horizontalLayout.addView(newDirButton);
titleLayout.addView(m_titleView);
titleLayout.addView(horizontalLayout);

提前致谢!

4

1 回答 1

1

尝试将 LayoutParams 设置为horizontalLayout.

无论如何,我建议转移到 xml 世界,因为这段代码是不可维护的。

编辑:

作者找到的答案:

titleLayout.setOrientation(LinearLayout.HORIZONTAL);

应该:

horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
于 2013-04-20T19:45:51.590 回答