我有一个用户在窗体中回答问题的项目。 我需要实现一个水平进度条(不是对话框),用点代替普通的条。Android - 带点的水平进度条
当用户回答问题时,该特定问题的点会改变颜色。 这可能吗?我正在使用API 16(目标)和API 7(最低)与Sherlock ActionBar
我有一个用户在窗体中回答问题的项目。 我需要实现一个水平进度条(不是对话框),用点代替普通的条。Android - 带点的水平进度条
当用户回答问题时,该特定问题的点会改变颜色。 这可能吗?我正在使用API 16(目标)和API 7(最低)与Sherlock ActionBar
如果您没有大量问题,如果您只是使用多个ImageView来表示点就可能就足够了。 XML的例子:
<RelativeLayout
android:id="@+id/dotsL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:orientation="vertical" >
<ImageView
android:id="@+id/dot1"
android:layout_width="5dp"
android:layout_height="5dp"
android:layout_marginTop="5dp"
android:background="@drawable/circle_white" >
</ImageView>
<ImageView
android:id="@+id/dot2"
android:layout_width="5dp"
android:layout_height="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@+id/dot1"
android:background="@drawable/circle_white" >
</ImageView>
<ImageView
android:id="@+id/dot3"
android:layout_width="5dp"
android:layout_height="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@+id/dot2"
android:background="@drawable/circle_white" >
</ImageView>
<ImageView
android:id="@+id/dot4"
android:layout_width="5dp"
android:layout_height="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@+id/dot3"
android:background="@drawable/circle_white" >
</ImageView>
<ImageView
android:id="@+id/dot5"
android:layout_width="5dp"
android:layout_height="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@+id/dot4"
android:background="@drawable/circle_white" >
</ImageView>
</RelativeLayout>
在java中:
mDots = new ImageView[5];
mDots[0] = (ImageView) rootView.findViewById(R.id.dot1);
mDots[1] = (ImageView) rootView.findViewById(R.id.dot2);
mDots[2] = (ImageView) rootView.findViewById(R.id.dot3);
mDots[3] = (ImageView) rootView.findViewById(R.id.dot4);
mDots[4] = (ImageView) rootView.findViewById(R.id.dot5);
当用户回答一个问题,刚才:
mDots[questionNumber].setBackgroundDrawable(getResources().getDrawable(R.drawable.circle_green));
注意:此方法要求你必须根据可绘制。
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建议。
Android框架已经包含一个ProgressBar小部件。这适用于所有类型的布局,并不限于对话框。使用它并指定android:progressDrawable或android:indeterminateDrawable属性并绘制自己的点将是最简单的解决方案。详情请参阅documentation。
试试这个https://stackoverflow.com/a/37741873/7104450 –