首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android -带点的水平进度条

Android -带点的水平进度条
EN

Stack Overflow用户
提问于 2012-11-08 20:04:24
回答 3查看 7.2K关注 0票数 2

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

当用户回答一个问题时,该特定问题的圆点会改变颜色。这是可能的吗?我正在使用API16(目标)和API7(最低)与夏洛克ActionBar

EN

回答 3

Stack Overflow用户

发布于 2012-11-08 20:21:22

如果你没有大量的问题,只要使用多个ImageViews来表示这些点就足够了。XML示例:

代码语言:javascript
运行
复制
 <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中:

代码语言:javascript
运行
复制
    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);

当用户回答问题时,只需:

代码语言:javascript
运行
复制
mDots[questionNumber].setBackgroundDrawable(getResources().getDrawable(R.drawable.circle_green));

注意:这种方法要求你有相应的绘图文件。

票数 1
EN

Stack Overflow用户

发布于 2012-11-08 21:28:58

XML:

代码语言:javascript
运行
复制
<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,如您所见,因此每个“点”具有相同的大小:

代码语言:javascript
运行
复制
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);
}

在每个答案后显示图片:

代码语言:javascript
运行
复制
imageLayout.getChildAt(questionNr).setVisibility(View.VISIBLE); //starting at 0

我只是改进了slezadav的建议。

票数 1
EN

Stack Overflow用户

发布于 2012-11-09 04:29:36

Android框架中已经包含了一个ProgressBar小部件。这在所有类型的布局中都有效,并不局限于对话框。使用它并指定android:progressDrawable或android:indeterminateDrawable属性并绘制自己的点将是最简单的解决方案。有关详细信息,请参阅documentation

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13288754

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档