[英]Placing buttons below each other programmatically in Android?
我想在彼此下方放置一些按钮。 我试过这样:
View previousView =null;
RelativeLayout evaluationSections = findViewById(R.id.evaluation_sections);
for (int i = 0; i < getQuestions().length(); i++) {
Button question = new Button(activity);
RelativeLayout.LayoutParams questionLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
if (previousView != null){
questionLayoutParams.addRule(RelativeLayout.BELOW,previousView.getId());
}
question.setLayoutParams(questionLayoutParams);
question.setId(ViewCompat.generateViewId());
question.setText("Question "+i);
evaluationSections.addView(question);
previousView = question;
for (int j=0;j< getAnswers().length();j++){
Button answer = new Button(activity);
RelativeLayout.LayoutParams answerLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
answer.setText("Answer "+j);
if (previousView != null){
answerLayoutParams.addRule(RelativeLayout.BELOW,previousView.getId());
}
answer.setLayoutParams(answerLayoutParams);
evaluationSections.addView(answer);
previousView = answer;
}
按钮彼此重叠,但应在彼此下方。 您知道它不起作用的原因是什么吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.