【发布时间】:2012-06-20 23:45:59
【问题描述】:
我需要在我的 ratingBar 的星星之间插入一个空格,例如 ratingbar 很好:
但我需要它:
我该怎么做?
【问题讨论】:
-
我添加了一个功能请求,请为它投票:code.google.com/p/android/issues/detail?id=230511
我需要在我的 ratingBar 的星星之间插入一个空格,例如 ratingbar 很好:
但我需要它:
我该怎么做?
【问题讨论】:
我认为您必须获取系统星形 png 文件的副本,并使用 photoshop / gimp / 其他一些编辑器手动添加您想要的填充。
【讨论】:
Drawable,使用大小属性来伪造填充(通过为RatingBar 设置style 标签)。
我同意蒂姆 我应用了相同的逻辑并且它起作用了。 在我的项目中,我使用自己的星图作为星图 我制作的星星图像在右侧有额外的空间(填充),导致星星之间有空间
【讨论】:
您有下一个属性。
android:progressDrawable = "@drawable/rating_stars"
android:indeterminateDrawable = "@drawable/rating_stars"
@drawable/rating_stars:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+android:id/background"
android:drawable="@drawable/star_empty" />
<item android:id="@+android:id/secondaryProgress"
android:drawable="@drawable/star_empty" />
<item android:id="@+android:id/progress"
android:drawable="@drawable/star" />
</layer-list>
star_empty 和 star 是您的可绘制目录中的图像。 因此,您可以在图形编辑器中更改 star 和 star_empty,并在需要时添加 spassing。
【讨论】:
只需为其使用自定义图标并使用样式,我的意思是 Photoshop 它,因为您希望它看起来并替换为系统评级样式图标。
【讨论】:
我不知道它是否还有用,但我创建了一个自定义库,允许您以编程方式和在 XML(以及其他内容)中更改空间 beetwen 星星:SimpleRatingBar。
它的特点:
android:layout_width:可以设置为wrap_content、match_parent或abritary dp。在你的情况下,你只需要这样做:
ratingbar.setStarsSeparation(20, Dimension.DP);
或者,例如,以像素为单位:
ratingbar.setStarsSeparation(100, Dimension.PX);
您可以在jcenter 或Maven Central 中找到它。因此,在您的 build.gradle 文件中,只需添加到您的依赖项:
compile 'com.iarcuschin:simpleratingbar:0.1.+'
【讨论】:
您可以使用自定义 SVG 并设置您的分隔值
通过使用该类,您可以修复Android自定义SVG RatingBar并通过替换类中的值(我将此值标记为There_You_Can_Set_Your_Value)来设置Drawable End。
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapShader
import android.graphics.Canvas
import android.graphics.Shader
import android.graphics.drawable.*
import android.graphics.drawable.shapes.RoundRectShape
import android.graphics.drawable.shapes.Shape
import android.util.AttributeSet
import android.view.Gravity
import androidx.appcompat.graphics.drawable.DrawableWrapper
import androidx.appcompat.widget.AppCompatRatingBar
class RatingBarSvg @JvmOverloads
constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = R.attr.ratingBarStyle,
) : AppCompatRatingBar(context, attrs, defStyleAttr) {
private var mSampleTile: Bitmap? = null
private val drawableShape: Shape
get() {
val roundedCorners = floatArrayOf(5f, 5f, 5f, 5f, 5f, 5f, 5f, 5f)
return RoundRectShape(roundedCorners, null, null)
}
init {
val drawable = tileify(progressDrawable, false) as LayerDrawable
//drawable.findDrawableByLayerId(android.R.id.background).setColorFilter(backgroundTintColor, PorterDuff.Mode.SRC_ATOP);
//drawable.findDrawableByLayerId(android.R.id.progress).setColorFilter(progressTintColor, PorterDuff.Mode.SRC_ATOP);
progressDrawable = drawable
}
/**
* Converts a drawable to a tiled version of itself. It will recursively
* traverse layer and state list drawables.
*/
@SuppressLint("RestrictedApi")
private fun tileify(drawable: Drawable, clip: Boolean): Drawable {
if (drawable is DrawableWrapper) {
var inner: Drawable? = drawable.wrappedDrawable
if (inner != null) {
inner = tileify(inner, clip)
drawable.wrappedDrawable = inner
}
} else if (drawable is LayerDrawable) {
val numberOfLayers = drawable.numberOfLayers
val outDrawables = arrayOfNulls<Drawable>(numberOfLayers)
for (i in 0 until numberOfLayers) {
val id = drawable.getId(i)
outDrawables[i] = tileify(
drawable.getDrawable(i),
id == android.R.id.progress || id == android.R.id.secondaryProgress
)
}
val newBg = LayerDrawable(outDrawables)
for (i in 0 until numberOfLayers) {
newBg.setId(i, drawable.getId(i))
}
return newBg
} else if (drawable is BitmapDrawable) {
val tileBitmap = drawable.bitmap
if (mSampleTile == null) {
mSampleTile = tileBitmap
}
val shapeDrawable = ShapeDrawable(drawableShape)
val bitmapShader = BitmapShader(
tileBitmap,
Shader.TileMode.REPEAT, Shader.TileMode.CLAMP
)
shapeDrawable.paint.shader = bitmapShader
shapeDrawable.paint.colorFilter = drawable.paint.colorFilter
return if (clip)
ClipDrawable(
shapeDrawable, Gravity.START,
ClipDrawable.HORIZONTAL
)
else
shapeDrawable
} else {
return tileify(getBitmapDrawableFromVectorDrawable(drawable), clip)
}
return drawable
}
private fun getBitmapDrawableFromVectorDrawable(drawable: Drawable): BitmapDrawable {
val bitmap = Bitmap.createBitmap(
drawable.intrinsicWidth + (**There_You_Can_Set_Your_Value**).toInt(), //dp between svg images //* resources.displayMetrics.density
drawable.intrinsicHeight,
Bitmap.Config.ARGB_8888
)
val canvas = Canvas(bitmap)
drawable.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight)
drawable.draw(canvas)
return BitmapDrawable(resources, bitmap)
}
@Synchronized
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
if (mSampleTile != null) {
val width = mSampleTile!!.width * numStars
setMeasuredDimension(
resolveSizeAndState(width, widthMeasureSpec, 0),
measuredHeight
)
}
}
}
【讨论】: