将一个进度条添加到自定义视图中

3
我找不到关于如何做这件事的提示或示例。我想在已经绘制的矩形上添加一个进度条,那么我该怎么做呢?
非常感谢你的回答! :) 编辑
public class MainView extends View {

    public MainView(Context context) {
        super(context);
    }

    public MainView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        logo.set(getPX(5), getPX(10), getPX(65), getPX(70));

        background.set(getPX(30), getPX(2), canvas.getWidth() - getPX(10),
                getPX(81));

        canvas.drawRect(background, paint);
        canvas.drawRect(logo, paint);
    }

    final private int getPX(float dp) {
        return (int) (getResources().getDisplayMetrics().density * dp);
    }
}

1
展示你的矩形代码。 - Boris Mocialov
progressBar.getProgressDrawable().setBounds(yourRect); //据我理解,您只是想根据矩形坐标重新定位进度条。 - Boris Mocialov
@MocialovBoris,我已经添加了它。很抱歉,但我认为无论是否添加都不会有什么好处。 - user2412629
yourCustomView.addView(new ProgressBar(activity, null, android.R.attr.progressBarStyleSmall)) - Boris Mocialov
让我们在聊天中继续这个讨论:http://chat.stackoverflow.com/rooms/32481/discussion-between-user222010-and-mocialov-boris - user2412629
显示剩余2条评论
1个回答

1
根据您展示给我的实现方式:
<ScrollView .. >
    <LinearLayout .. >
        <MainView .. />
        <MainView .. />
        <MainView .. />
    </LinearLayout>
</ScrollView>

我的建议是 - 将您的CustomView类扩展为LinearLayout(因为这样您可以将其他视图添加到您的自定义视图):
public class MainView extends LinearLayout {
    private Rect logo;
    private Rect background;

    public MainView(Context context) {
        super(context);

        setWillNotDraw(false);  //needed in order to call onDraw method

        logo = new Rect();
        background = new Rect();
    }

    public MainView(Context context, AttributeSet attrs) {
        super(context, attrs);

        setWillNotDraw(false);

        logo = new Rect();
        background = new Rect();

        RelativeLayout progressBarLayout = new RelativeLayout(context);
        RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.FILL_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        ProgressBar progressBar = new ProgressBar(context, null, R.attr.progressBarStyleHorizontal);
        progressBar.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
        progressBar.setIndeterminate(true); //remove that (only for demonstration purposes)
        lay.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        progressBarLayout.addView(progressBar, lay);

        addView(progressBarLayout);

       //Apart from the ProgressBar you are able to add as many views as you want to your custom view and align them as you would like
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        logo.set(getPX(5), getPX(10), getPX(65), getPX(70));

        background.set(getPX(30), getPX(2), canvas.getWidth() - getPX(10),
                getPX(81));

        canvas.drawRect(background, paint);
        canvas.drawRect(logo, paint);
    }

    final private int getPX(float dp) {
        return (int) (getResources().getDisplayMetrics().density * dp);
    }
}

在这个例子中,我只展示了如何添加 ProgressBar 组件,因为那是您最初的问题。为了实现您的要求(从提问者那里得到),您需要添加一些代码。P.S.这只是针对您特定问题/请求的解决方案。我建议使用 ListView 组件,它在重用视图方面更好,因此在您将有大量自定义视图实例的情况下,您的应用程序可能会变得无法使用,因为活动类上的负载太大。为了迁移到使用 ListView 组件,首先尝试一些示例,例如 this

非常感谢。我只需要做一些代码和稍微的更改就能让它工作了。再次感谢。 - user2412629

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接