目前我有一个按钮列表显示在ScrollView
但是所有按钮都在左侧,并在一列中显示。
我希望这些按钮以3列显示,因此当我动态添加行时,在一行中有3个按钮
以下是我必须显示这些按钮的当前代码
// Find the ScrollView
ScrollView scrollView = (ScrollView)
findViewById(R.id.scrollView);
// Create a LinearLayout element
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
// Add Buttons
for (int i = 0; i < 3; i++) {
linearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 20; j++) {
Button button = new Button(this);
button.setText("Some text");
linearLayout.addView(button);
}
}
// Add the LinearLayout element to the ScrollView
scrollView.addView(linearLayout);
}
我尝试设置按钮的布局参数,但它没有改变任何东西
有什么想法吗?
由于
答案 0 :(得分:1)
试试这个逻辑
// Find the ScrollView
ScrollView scrollView = (ScrollView)findViewById(R.id.scrollView);
// Create a LinearLayout element
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
// Add Buttons
for (int i = 0; i < 20; i++) {
LinearLayout linearLayoutChild = new LinearLayout(this);
linearLayoutChild.setOrientation(LinearLayout.HORIZONTAL);
linearLayoutChild.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 3; j++) {
Button button = new Button(this);
button.setText("Some text");
linearLayoutChild.addView(button);
}
linearLayout.addView(linearLayoutChild);
}
// Add the LinearLayout element to the ScrollView
scrollView.addView(linearLayout);