以编程方式在xml相同代码中绘制形状

时间:2016-02-27 20:41:09

标签: android android-drawable kotlin xml-drawable

我有以下xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
  <solid android:color="@color/coral_seconds"/>

  <size
      android:height="24dp"
      android:width="24dp"/>
</shape>

如何将此xml作为一个初级Drawable对象?

我没有成功(Kotlin):

val circle = ShapeDrawable(OvalShape())

circle.paint.color = ContextCompat.getColor(context, resId)
circle.paint.style = Paint.Style.FILL;

答案可以是Java,我不介意。

1 个答案:

答案 0 :(得分:1)

看起来大小可以通过intrinsicDIMEN值设置,例如:

val circle = ShapeDrawable(OvalShape())

circle.intrinsicHeight = dpi24
circle.intrinsicWidth = dpi24

circle.paint.color = ContextCompat.getColor(context, resId)
circle.paint.style = Paint.Style.FILL;