XML:
<LinearLayout
android:id="@+id/image_layout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical">
动态添加图片并隐藏它们(基于问题总数),我也将 layout_weight 设置为 1,如您所见,因此每个“点”具有相同的大小:
ImageView[] images = new ImageView[totQuestions];
LinearLayout imageLayout = (LinearLayout) findViewById(R.id.image_layout);
ImageView image;
LinearLayout.LayoutParams layoutParams;
for(int i = 0; i < totQuestions; i++){
image = new ImageView(this);
layoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1.0f);
image.setLayoutParams(layoutParams);
image.setBackgroundDrawable(getResources().getDrawable(R.drawable.circle_green));
image.setVisibility(View.INVISIBLE);
images[i] = image;
imageLayout.addView(image);
}
每个答案后显示图片:
imageLayout.getChildAt(questionNr).setVisibility(View.VISIBLE); //starting at 0
我只是改进了slezadav 的建议。