我想连续实现5个复选框,并在另一个下面实现3个这样的行。 附件是图像。
如何使用CheckBox,LinearLayout和TableRow做到这一点? 所有复选框都是使用String数组动态创建的,而不是在布局xml文件内部创建的。
我尝试了以下操作:
int i = 0;
while(i < 15) {
TableRow tr = new TableRow(this);
CheckBox[] cb = new CheckBox[5];
for (int j = 0; j < 5; j++,i++) {
cb[j] = new CheckBox(this); cb[j].setText(arrLetters[i]);
cb[j].setHeight(80);
cb[j].setWidth(250);
cb[j].setId(i);
tr.addView(cb[j]);
}
linearLayout.addView(tr);
abhishek
答案 0 :(得分:0)
您实际上不需要TableRow
即可实现。您可以像这样使用LinearLayout
:
LinearLayout
。LinearLayout
s添加到水平布局。CheckBox
。CheckBox
的 weight 设置为 1 。示例
动态创建LinearLayout
:
LinearLayout linLayout = new LinearLayout(this);
// specifying vertical or horizontal orientation
linLayout.setOrientation(LinearLayout.VERTICAL);
// creating LayoutParams
LayoutParams linLayoutParam = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);
动态创建CheckBox
并将其添加到LinearLayout
:
CheckBox checkBox = new CheckBox(this);
// add other attributes like id and Params for the weight..etc
linLayout.addView(checkBox, linLayoutParam);
有效的实施方式(示例)
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.CheckBox;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout horizontalLayout = new LinearLayout(this);
horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
horizontalLayout.setGravity(Gravity.CENTER);
setContentView(horizontalLayout, layoutParams);
int[] colors = new int[]{ Color.BLUE, Color.WHITE, Color.RED, Color.GRAY, Color.GREEN};
LinearLayout [] columns = new LinearLayout[5];
for(int i=0; i<columns.length; i++){
LinearLayout verticalLayout = new LinearLayout(this);
verticalLayout.setOrientation(LinearLayout.VERTICAL);
verticalLayout.setGravity(Gravity.CENTER);
verticalLayout.setId(i);
verticalLayout.setBackgroundColor(colors[i]);
columns[i] = verticalLayout;
horizontalLayout.addView(verticalLayout, layoutParams);
}
String[] checkBoxesNames = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
"K", "L", "M", "N", "O"};
CheckBox[] checkBoxes = new CheckBox[checkBoxesNames.length];
for(int i=0; i<checkBoxes.length; i++){
CheckBox checkBox = new CheckBox(this);
checkBox.setText(checkBoxesNames[i].toString());
checkBox.setId(i);
checkBox.setLayoutParams(layoutParams);
if(i%5==0) {columns[0].addView(checkBox, layoutParams);} // column 0
if(i%5==1) {columns[1].addView(checkBox, layoutParams);} // column 1
if(i%5==2) {columns[2].addView(checkBox, layoutParams);} // column 2
if(i%5==3) {columns[3].addView(checkBox, layoutParams);} // column 3
if(i%5==4) {columns[4].addView(checkBox, layoutParams);} // column 4
}
}
}
结果 [仅用于演示的颜色!]
请注意,以上仅是示例,但您可以按照适合应用程序的方式操作 Vertical LinearLayout
(即列),例如:如果我将参数从MATCH_PARENT
更改为WRAP_CONTENT
,或者如果我设置了特定尺寸,则外观将如下所示:
答案 1 :(得分:0)