/ / android ProgressBar setProgressDrawable nie działa poprawnie - android, pasek postępu android

android ProgressBar setProgressDrawable nie działa poprawnie - android, pasek postępu android

kiedy używam Androida: progressDrawable = "@ drawable / barcolor", działa poprawnie, ale gdy używam setProgressDrawable, wygląda to źle mój układ:

<?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>

i moje kody 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);

i 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>

Dziękuję wam wszystkim !

Odpowiedzi:

0 dla odpowiedzi № 1

Spróbuj tego:

     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);
}
}
});

0 dla odpowiedzi nr 2

hoomi, dziękuję za pomoc! Rozwiązałem ten problem za pomocą niestandardowego paska ProgressBar, wywołuje 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();
}

}