我下面的drawableStart
中有一个可绘制的Button
图标,但无法正确缩放。我该怎么做才能纠正这个问题?
<Button
android:id="@+id/show_in_map_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="8dp"
android:background="@drawable/round_details_button"
android:drawableStart="@drawable/location_btn"
android:drawableTint="@android:color/white"
android:fontFamily="@font/montserrat_medium"
android:paddingStart="15dp"
android:paddingEnd="15dp"
android:text="@string/show_in_map_button"
android:textColor="@android:color/white"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/place_description" />
P.S:我看过一些有关此问题的帖子,试图使用ScaleDrawable
在代码中解决此问题,但这对我不起作用。
答案 0 :(得分:1)
有一种老方法对我有用。
我只是创建边界并将其设置为我的按钮。这样,我附加到我的可绘制对象上的所有可绘制对象都具有边界的尺寸。
drawable.bounds = Rect(0, 0, 80, 80) // in this case, the drawable is going to be 80 x 80
button.setCompoundDrawables(drawable, null, null, null)
我希望这会有所帮助。快活的编码!
答案 1 :(得分:0)
您已经尝试过了,您可以像这样缩放您的可绘制对象。
fixture.debugElement.nativeElement
您还可以像这样缩放您的可绘制对象
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/logo"
android:scaleGravity="center_vertical|center_horizontal"
android:scaleHeight="80%"
android:scaleWidth="80%" />
答案 2 :(得分:0)
一种可能的解决方案是将您的资源包装在一个drawable中,并在其中定义高度和宽度,然后在android:drawableStart
属性中使用该drawable。这仅适用于API 23及更高版本,但是例如,它不会使您的应用在API 21上崩溃。您可以看下面的示例:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/location_btn"
android:width="@dimen/icon_size"
android:height="@dimen/icon_size" />
</layer-list>
答案 3 :(得分:0)
也尝试使用可绘制的填充。
答案 4 :(得分:0)
要在Button
或TextView
中使用自定义大小的 drawable ,可以使用类似的样式属性创建如下的自定义类:
在 res->值文件夹中创建 attrs.xml 文件,并创建如下所示的 styleable :
<resources>
<declare-styleable name="DrawableButton">
<attr name="drawableSize" format="dimension" />
<attr name="drawableScale" format="float" />
</declare-styleable>
</resources>
然后创建名为DrawableButton
的类,如下所示:
class DrawableButton : AppCompatButton {
internal var drawableSize: Float? = null
internal var drawableScale: Float = 1F
constructor(context: Context?) : this(context, null)
constructor(context: Context?, attr: AttributeSet?) : this(context, attr, android.R.style.Widget_Button)
constructor(context: Context?, attr: AttributeSet?, defStyle: Int) : super(context, attr, defStyle) {
loadAttrs(context, attr)
initDrawablesForResize()
}
private fun loadAttrs(context: Context?, attrs: AttributeSet?) {
if (context != null && attrs != null) {
val typedArray: TypedArray? = context.obtainStyledAttributes(attrs, R.styleable.DrawableButton, 0, 0)
typedArray?.let { arr ->
for (index in 0 until arr.indexCount) {
assignValueToVariables(arr, index)
}
}
typedArray?.recycle()
}
}
private fun assignValueToVariables(arr: TypedArray, index: Int) {
val resourceId = arr.getIndex(index)
when (resourceId) {
R.styleable.DrawableButton_drawableSize -> {
drawableSize = arr.getDimension(resourceId, Math.min(measuredHeight, measuredWidth).toFloat())
}
R.styleable.DrawableButton_drawableScale -> {
drawableScale = arr.getFloat(resourceId, 1F)
}
else -> {
// Left out because "Unused"
}
}
}
private fun initDrawablesForResize() {
val size = compoundDrawablesRelative.size
val drawableList = ArrayList<Drawable?>(size)
for (index in 0 until size) {
val drawable = compoundDrawablesRelative[index]
if (drawable != null) {
if (drawableSize == null) {
drawableSize = Math.min(drawable.intrinsicHeight.times(drawableScale), drawable.intrinsicWidth.times(drawableScale))
}
drawable.setBounds(
0,
0,
drawableSize!!.toInt(),
drawableSize!!.toInt()
)
drawableList.add(index, drawable)
} else {
drawableList.add(index, null)
}
}
this.setCompoundDrawablesRelative(drawableList[0], drawableList[1], drawableList[2], drawableList[3])
}
override fun performClick(): Boolean {
super.performClick()
return true
}
}
重建项目,现在您可以在XML文件中使用此DrawableButton
类,如下所示:
<your_package_name_where_this_class_is.DrawableButton
android:id="@+id/show_in_map_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="8dp"
android:background="@drawable/round_details_button"
android:drawableStart="@drawable/location_btn"
android:drawableTint="@android:color/white"
android:fontFamily="@font/montserrat_medium"
android:paddingStart="15dp"
android:paddingEnd="15dp"
android:text="@string/show_in_map_button"
android:textColor="@android:color/white"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/place_description" />
现在,您可以使用 drawableSize
或其他自定义命名空间中的 drawableScale
或 app:
属性在 drawableStart
,drawableEnd
,drawableTop
或drawableBottom
中缩放图形。它适用于矢量和光栅图像。
注意:由于方法
setCompoundDrawablesRelative()
对可绘制对象使用了开始和结束属性,因此它与drawableLeft和drawableRight一起不起作用。要使用左右可绘制对象,请用setCompoundDrawables()
方法替换此方法。