android ProgressBar setProgressDrawable无法正常工作

时间:2015-03-16 13:10:47

标签: android android-progressbar

当我使用android:progressDrawable =“@ drawable / barcolor”时,它工作正常,但是当我使用setProgressDrawable时,它看起来不对 我的布局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 

<ProgressBar
    android:id="@+id/firstProgressBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="300dip"
    android:layout_height="30dip"
    android:layout_centerHorizontal="true"
    android:layout_above="@+id/text_first"
    android:max="300"
    android:progress="0" />

<TextView  
    android:id="@+id/text_first"
    android:textColor="#fff"
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="10dp"
    android:gravity="center"
/>
</RelativeLayout> 

和我的java代码:

    firstProgressBar = (ProgressBar)findViewById(resId4);
    resId = getResources().getIdentifier("barcolor","drawable",packageName);
    Rect bounds = firstProgressBar.getProgressDrawable().getBounds();
    firstProgressBar.setProgressDrawable(getResources().getDrawable(resId));
    firstProgressBar.getProgressDrawable().setBounds(bounds);

和barcolor.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"> 
<nine-patch android:src="@drawable/progress_bar_bg"> 
</nine-patch>
</item>
<item android:id="@android:id/progress"> 
<clip> 
   <nine-patch android:src="@drawable/progress_bar">
   </nine-patch> 
</clip>
</item>
</layer-list>

谢谢大家!

2 个答案:

答案 0 :(得分:0)

试试这个:

     firstProgressBar.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect bounds = firstProgressBar.getProgressDrawable().getBounds();
                Drawable drawable = getResources().getDrawable(resId);
                drawable.setBounds(bounds);
                firstProgressBar.setProgressDrawable(drawable);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    firstProgressBar.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                } else {
                    firstProgressBar.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }
            }
        });

答案 1 :(得分:0)

霍米,谢谢你的帮助!我用自定义ProgressBar解决了这个问题,它调用了CustomPorgressBar:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.util.AttributeSet;
import android.widget.ProgressBar;

public class CustomProgressBar extends ProgressBar {

    public CustomProgressBar(Context context, AttributeSet attrs, int   defStyle) {
            super(context, attrs, defStyle);
    }

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

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

    @Override
    public void draw(Canvas canvas) {
            Drawable d = getProgressDrawable();
            if (d instanceof LayerDrawable) {
                    LayerDrawable layerDrawable = (LayerDrawable) d;
                    Drawable progressDrawable = layerDrawable
                                    .findDrawableByLayerId(android.R.id.progress);
                    //
                    layerDrawable.setBounds(0, 0, progressRight, progressBottom);
                    progressDrawable.setBounds(0, 0, progressRight, progressBottom);
            }
            super.draw(canvas);
    }

    private int progressRight;
    private int progressBottom;

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
            progressRight = getWidth() - getPaddingRight() - getPaddingLeft();
            progressBottom = getHeight() - getPaddingBottom() - getPaddingTop();
    }

}
相关问题