使用 ProgressBar.setProgressDrawable 的问题

发布于 2024-11-26 23:11:56 字数 2779 浏览 0 评论 0原文

对具有自定义颜色的 ProgressBar 使用 setProgressDrawable 对我们来说并不能以正确的方式工作。我们在 ListView 的行中使用进度条,但仅当列表中存在多个元素时才会显示进度。如果只有一个元素,进度条为空。

CursorAdapter.java:

public class CursorAdapter extends SimpleCursorAdapter {
    public CursorAdapter(Context context, int layout, Cursor c, String[] from,
            int[] to) {
        super(context, layout, c, from, to);
    }

    public void bindView(View view, Context context, Cursor cursor) {
        super.bindView(view, context, cursor);
        updateProgressbar(view, cursor);
    }

    /**
     * This method updates the progressbar using the "numberpages" and
     * "currentpage" values. 
     */
    private void updateProgressbar(View view, Cursor cursor) {
        ProgressBar progressBar = (ProgressBar) view
                .findViewById(R.id.progressbarHorizontal);

        progressBar.setProgressDrawable(view.getResources().getDrawable(
                R.drawable.greenprogress));

        progressBar.setMax(cursor.getInt(cursor.getColumnIndex("numberpages")));
        progressBar.setProgress(cursor.getInt(cursor
                .getColumnIndex("currentpage")));
    }

}

/res/drawable/greenprogress.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
    <shape>
        <corners android:radius="5dip" />
        <gradient
                android:startColor="#ff9d9e9d"
                android:centerColor="#ff5a5d5a"
                android:centerY="0.75"
                android:endColor="#ff747674"
                android:angle="270"
        />
    </shape>
</item>

<item android:id="@android:id/secondaryProgress">
    <clip>
        <shape>
            <corners android:radius="5dip" />
            <gradient
                    android:startColor="#80ffd300"
                    android:centerColor="#80ffb600"
                    android:centerY="0.75"
                    android:endColor="#a0ffcb00"
                    android:angle="270"
            />
        </shape>
    </clip>
</item>

<item android:id="@android:id/progress">
    <clip>
        <shape>
            <corners
                android:radius="5dip" />
            <gradient
                android:startColor="#33FF33"
                android:endColor="#008000"
                android:angle="270" />
        </shape>
    </clip>
</item>

</layer-list>

这段代码有什么问题,为什么只有当列表中有多个元素时它才起作用?如果没有设置自定义 ProgressBar 样式,一切都会正常运行。 setProgressDrawable 方法似乎会出现问题。

谢谢你帮助我们。

Using setProgressDrawable for a ProgressBar with custom colors does not work in a correct way for us. We use progressbars in the rows of a ListView, but the progress is only displayed if there is more than one element in the list. In case of one element the progressbar is empty.

CursorAdapter.java:

public class CursorAdapter extends SimpleCursorAdapter {
    public CursorAdapter(Context context, int layout, Cursor c, String[] from,
            int[] to) {
        super(context, layout, c, from, to);
    }

    public void bindView(View view, Context context, Cursor cursor) {
        super.bindView(view, context, cursor);
        updateProgressbar(view, cursor);
    }

    /**
     * This method updates the progressbar using the "numberpages" and
     * "currentpage" values. 
     */
    private void updateProgressbar(View view, Cursor cursor) {
        ProgressBar progressBar = (ProgressBar) view
                .findViewById(R.id.progressbarHorizontal);

        progressBar.setProgressDrawable(view.getResources().getDrawable(
                R.drawable.greenprogress));

        progressBar.setMax(cursor.getInt(cursor.getColumnIndex("numberpages")));
        progressBar.setProgress(cursor.getInt(cursor
                .getColumnIndex("currentpage")));
    }

}

/res/drawable/greenprogress.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
    <shape>
        <corners android:radius="5dip" />
        <gradient
                android:startColor="#ff9d9e9d"
                android:centerColor="#ff5a5d5a"
                android:centerY="0.75"
                android:endColor="#ff747674"
                android:angle="270"
        />
    </shape>
</item>

<item android:id="@android:id/secondaryProgress">
    <clip>
        <shape>
            <corners android:radius="5dip" />
            <gradient
                    android:startColor="#80ffd300"
                    android:centerColor="#80ffb600"
                    android:centerY="0.75"
                    android:endColor="#a0ffcb00"
                    android:angle="270"
            />
        </shape>
    </clip>
</item>

<item android:id="@android:id/progress">
    <clip>
        <shape>
            <corners
                android:radius="5dip" />
            <gradient
                android:startColor="#33FF33"
                android:endColor="#008000"
                android:angle="270" />
        </shape>
    </clip>
</item>

</layer-list>

What's wrong with this code and why does it work only if there are more than one elements in the list? Without setting a custom ProgressBar style everything is running fine. The method setProgressDrawable seems to make problems.

Thank you for helping us.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零度° 2024-12-03 23:11:56
private void updateProgressbar(View view, Cursor cursor) {
        ProgressBar progressBar = (ProgressBar) view
                .findViewById(R.id.progressbarHorizontal);

        progressBar.setProgressDrawable(view.getResources().getDrawable(
                R.drawable.greenprogress));

> progressBar.setProgress(1); "Add this statement before setting the progress.."

        progressBar.setMax(cursor.getInt(cursor.getColumnIndex("numberpages")));
        progressBar.setProgress(cursor.getInt(cursor
                .getColumnIndex("currentpage")));
    }
private void updateProgressbar(View view, Cursor cursor) {
        ProgressBar progressBar = (ProgressBar) view
                .findViewById(R.id.progressbarHorizontal);

        progressBar.setProgressDrawable(view.getResources().getDrawable(
                R.drawable.greenprogress));

> progressBar.setProgress(1); "Add this statement before setting the progress.."

        progressBar.setMax(cursor.getInt(cursor.getColumnIndex("numberpages")));
        progressBar.setProgress(cursor.getInt(cursor
                .getColumnIndex("currentpage")));
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文