我正在从API中获取一些数据,并生成Android布局,但无法实现漂亮的布局,如下所示。
我希望TextView-> RadioGroup在TextView下面,以获取我收到的每个新数据。总是一个问题,一个广播小组。
这是我的代码:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_checklist);
Intent intent = getIntent();
int size = intent.getIntExtra("size", 0);
Log.d(TAG, "onCreate - Qtd Questões: " + size);
ScrollView scrollView= new ScrollView(this);
RelativeLayout layout = new RelativeLayout(this);
for (int i=0; i<size;i++) {
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(layoutParams);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
TextView tv1 = new TextView(this);
tv1.setId(i+1);
tv1.setText(intent.getStringExtra("q"+i));
params1.addRule(RelativeLayout.BELOW, tv1.getId());
// Initialize a new RadioGroup
RadioGroup rg = new RadioGroup(getApplicationContext());
rg.setOrientation(RadioGroup.HORIZONTAL);
params2.addRule(RelativeLayout.BELOW, tv1.getId());
rg.setId(i+1);
// Create a Radio Button for RadioGroup
RadioButton rb_sim = new RadioButton(getApplicationContext());
rb_sim.setText("Sim");
rb_sim.setId(i+i);
rg.addView(rb_sim);
RadioButton rb_nao = new RadioButton(getApplicationContext());
rb_nao.setText("Não");
rb_nao.setId(i+i);
rg.addView(rb_nao);
RadioButton rb_na = new RadioButton(getApplicationContext());
rb_na.setText("Não se Aplica");
rb_na.setId(i+i);
rg.addView(rb_na);
if (i > 0) {
params1.addRule(RelativeLayout.BELOW, rg.getId());
layout.addView(tv1,params1);
} else {
layout.addView(tv1);
}
layout.addView(rg, params2);
}
scrollView.addView(layout);
setContentView(scrollView);
}
可能我已经接近了,但是我看不出有什么问题。
答案 0 :(得分:1)
尝试将“相对”布局更改为“线性”,然后检查输出。
RelativeLayout layout = new RelativeLayout(this);
收件人
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
希望这可以正常显示您的视图,而不会与textView重叠。