我的代码是我写的:
while (i.hasNext()){
Map.Entry me= (Map.Entry) i.next();
checkBox=new CheckBox(this.getActivity());
checkBox.setId(Integer.parseInt(me.getKey().toString()));
checkBox.setText(me.getValue().toString());
checkBox.setOnClickListener(getOnClickDoSomething(checkBox));
LinearLayout.LayoutParams params= (LinearLayout.LayoutParams) linearMain.getLayoutParams();
params.width=250;
linearMain.addView(checkBox);
dltButton=new Button(this.getActivity());
dltButton.setTag(btnCounter);
btnCounter++;
dltButton.setText("Delete");
linearMain.addView(dltButton);
}
我想要一个复选框和一个删除按钮,并且下一行中的下一行类似。 如何实现它?
答案 0 :(得分:0)
好吧试试这个,我已经将你的代码修改为基本逻辑。你的活动布局文件应如下所示
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout >
然后你的活动应该是
public class MainActivity extends AppCompatActivity {
LinearLayout mainContainer;
CheckBox checkBox;
Button dltButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainContainer = (LinearLayout) findViewById(R.id.mainContainer);
for (int i = 0; i < 5 ; i++){
checkBox=new CheckBox(this);
checkBox.setText("Text");
LinearLayout linearMain = new LinearLayout(this);
linearMain.setOrientation(LinearLayout.HORIZONTAL);
linearMain.setWeightSum(1.0f);
LinearLayout.LayoutParams half = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 0.5f);
linearMain.addView(checkBox, half);
dltButton=new Button(this);
dltButton.setTag("tag");
dltButton.setText("ffff");
linearMain.addView(dltButton, half);
mainContainer.addView(linearMain);
}
}
答案 1 :(得分:0)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout);
layout.setOrientation(LinearLayout.VERTICAL);
String[] count = new String[] { "1", "2", "3" };
int checkbox_count = 3;
CheckBox checkBox;
for (int i = 0; i < checkbox_count; i++) {
LinearLayout horizontal_layout = new LinearLayout(this);
horizontal_layout.setOrientation(LinearLayout.HORIZONTAL);
horizontal_layout.setLayoutParams(new LinearLayout.LayoutParams(500, 80));
checkBox = new CheckBox(this);
checkBox.setText("checkbox " + count[i]);
horizontal_layout.addView(checkBox);
Button delete_btn = new Button(this);
delete_btn.setText("Delete");
horizontal_layout.addView(delete_btn);
layout.addView(horizontal_layout);
}
}