ratingBar = (RatingBar) inflater.inflate(R.layout.ratingbar, null);
ratingBar.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
布局:
<?xml version="1.0" encoding="utf-8"?>
<RatingBar xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/myStyle" android:layout_alignParentRight="true"
android:layout_height="19dp" android:layout_width="wrap_content"
android:numStars="5">
</RatingBar>
风格:
<style name="myStyle" parent="@android:style/Widget.RatingBar">
<item name="android:progressDrawable">@drawable/android_r2_ratingstar_yellow</item>
<item name="android:indeterminateDrawable">@drawable/android_r2_ratingstar_grey</item>
</style>
谢谢,欢呼
答案 0 :(得分:3)
您必须将填充添加到png本身。虽然有一种可以将你的drawable包装成Inset Drawable(http://developer.android.com/guide/topics/resources/drawable-resource.html#Inset)的东西,但是'tileify'的代码(如android源代码中所述)不处理drawable是插入drawable的情况,因此不会平铺图像。
这是tilefy方法(在ProgressBar.java中,它是RatingBar.java的祖先:https://gist.github.com/CyanogenMod/android_frameworks_base/blob/gingerbread/core/java/android/widget/ProgressBar.java)
private Drawable tileify(Drawable drawable, boolean clip) {
if (drawable instanceof LayerDrawable) {
LayerDrawable background = (LayerDrawable) drawable;
final int N = background.getNumberOfLayers();
Drawable[] outDrawables = new Drawable[N];
for (int i = 0; i < N; i++) {
int id = background.getId(i);
outDrawables[i] = tileify(background.getDrawable(i),
(id == R.id.progress || id == R.id.secondaryProgress));
}
LayerDrawable newBg = new LayerDrawable(outDrawables);
for (int i = 0; i < N; i++) {
newBg.setId(i, background.getId(i));
}
return newBg;
} else if (drawable instanceof StateListDrawable) {
StateListDrawable in = (StateListDrawable) drawable;
StateListDrawable out = new StateListDrawable();
int numStates = in.getStateCount();
for (int i = 0; i < numStates; i++) {
out.addState(in.getStateSet(i), tileify(in.getStateDrawable(i), clip));
}
return out;
} else if (drawable instanceof BitmapDrawable) {
final Bitmap tileBitmap = ((BitmapDrawable) drawable).getBitmap();
if (mSampleTile == null) {
mSampleTile = tileBitmap;
}
final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape());
final BitmapShader bitmapShader = new BitmapShader(tileBitmap,
Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
shapeDrawable.getPaint().setShader(bitmapShader);
return (clip) ? new ClipDrawable(shapeDrawable, Gravity.LEFT,
ClipDrawable.HORIZONTAL) : shapeDrawable;
}
return drawable;
}
答案 1 :(得分:0)
没有简单的方法来修改图像之间的填充,更好地节省时间,只需用任何可用的图像编辑器添加一些空间。