如何在多行中设置相对布局中的多个按钮
How to set multiple buttons in Relative Layout in multiple lines
我以编程方式创建了一个布局,并在其中动态创建了多个按钮。我希望当屏幕结束时,其余按钮显示在上方按钮的下方。请帮我一个忙。
感谢任何帮助。提前致谢。
如果您动态创建根布局,则必须动态设置根布局的 layoutParams。
LinearLayout ll = new LinearLayout(mContext);
ll.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ll.setLayoutParams(layoutParams);
并将视图添加到此布局
Button btn=new Button(this);
ll.addView(btn, layoutParams);
没有测试过请试试这个代码
更新
RelativeLayout lLRoot = (RelativeLayout) findViewById(R.id.container);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
lLRoot.addView(ll);
Button btn=new Button(this);
btn.setText("hai");
ll.addView(btn);
我以编程方式创建了一个布局,并在其中动态创建了多个按钮。我希望当屏幕结束时,其余按钮显示在上方按钮的下方。请帮我一个忙。
感谢任何帮助。提前致谢。
如果您动态创建根布局,则必须动态设置根布局的 layoutParams。
LinearLayout ll = new LinearLayout(mContext);
ll.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ll.setLayoutParams(layoutParams);
并将视图添加到此布局
Button btn=new Button(this);
ll.addView(btn, layoutParams);
没有测试过请试试这个代码
更新
RelativeLayout lLRoot = (RelativeLayout) findViewById(R.id.container);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
lLRoot.addView(ll);
Button btn=new Button(this);
btn.setText("hai");
ll.addView(btn);