我需要访问ClipDrawable来设置片段级别。这是可绘制的:

@drawable/left_arrow

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <clip
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:drawable="@drawable/rotated_square"
      android:clipOrientation="horizontal"
      android:gravity="left" />
  </item>
</selector>


@drawable/rotated_square

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <rotate
      android:fromDegrees="45"
      android:pivotX="50%"
      android:pivotY="50%"
      android:toDegrees="45">
      <shape
        android:shape="rectangle">
        <size
          android:width="250dp"
          android:height="250dp"/>
        <solid
          android:color="#ffffff"/>
      </shape>
    </rotate>
  </item>
</selector>


当我尝试以left_arrow的形式检索可绘制的ClipDrawable时,如下所示:

ClipDrawable drawable = (ClipDrawable) getResources().getDrawable(R.drawable.left_arrow);


我在LogCat中收到以下错误:

Caused by: java.lang.ClassCastException: android.graphics.drawable.StateListDrawable cannot be cast to android.graphics.drawable.ClipDrawable
        at com.vcapra1.pingpongscoreboard.MainActivity.onCreate(MainActivity.java:89)


为什么我的ClipDrawable作为StateListDrawable返回?

最佳答案

您的left_arrowStateListDrawable,因为它的根元素是<selector>。如果希望将其作为ClipDrawable,则应直接使用<clip>元素作为XML的根,而不使用<selector><item>标记。

关于java - ClipDrawable getDrawable()返回一个StateListDrawable,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27585329/

10-09 06:56