【问题标题】:Android - Horizontal progress bar with dotsAndroid - 带点的水平进度条
【发布时间】:2012-11-08 12:04:23
【问题描述】:

我有一个项目,用户在表单中回答问题。 我需要实现一个水平进度条(不是对话框),用点代替普通条。

当用户回答问题时,该特定问题的圆点会改变颜色。 这个有可能?我将 API 16(目标)和 API 7(最低)与 Sherlock ActionBar 一起使用

【问题讨论】:

标签: android progress-bar android-progressbar


【解决方案1】:

如果您没有大量问题,则仅使用多个 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));

注意:这种方法需要你有相应的drawables。

【讨论】:

  • 我认为如果我已经知道用户将回答的问题数量,但问题的数量不在我的控制范围内,用户在网站中创建表单,他可以创建一个包含 10 个问题或 100 个问题的表单。我不能在 xml 中做静态点数,我必须用 N 因子以编程方式实例化它们
  • 在这种情况下你是对的,你必须以编程方式实例化 ImageViews,这当然是可能的。也许不是解决这个问题的最干净的方法,但它应该可以工作。
【解决方案2】:

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 的建议。

【讨论】:

  • 我会测试一下,明天我会发布反馈,tkz!
  • 我还没有测试过这个,但我很确定它可以工作。这可能是一些小问题。
【解决方案3】:

Android 框架中已经包含一个 ProgressBar 小部件。这适用于所有类型的布局,并且不限于对话框。使用它并使用您自己的可绘制点指定 android:progressDrawable 或 android:indeterminateDrawable 属性将是最简单的解决方案。详情请见documentation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-01
    • 2012-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多