Jul*_*lia 5 android drawable android-layout android-drawable android-color
我有可绘制的 xml up.xml,如下面的代码所示
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<rotate
android:fromDegrees="45"
android:toDegrees="45"
android:pivotX="-40%"
android:pivotY="87%" >
<shape
android:shape="rectangle" >
<solid
android:color="@color/green" />
</shape>
</rotate>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
我将这个 drawable 附加到一个按钮上,如下所示
<Button android:id="@+id/bill_amount_up"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/up"
/>
Run Code Online (Sandbox Code Playgroud)
我正在尝试在我的代码中动态更改 up.xml 文件中纯色 android:color="@color/green" 的颜色。我尝试了以下方法,但没有奏效。
((GradientDrawable)billAmountUp.getBackground()).setColor(color);
Run Code Online (Sandbox Code Playgroud)
我收到以下错误 java.lang.ClassCastException: android.graphics.drawable.LayerDrawable cannot be cast to android.graphics.drawable.GradientDrawable
任何人都可以帮忙吗?谢谢你。
试试这个我的朋友
Drawable myIcon = getResources().getDrawable( R.drawable.button );
ColorFilter filter = new LightingColorFilter( Color.BLACK, Color.BLACK);
myIcon.setColorFilter(filter);
Run Code Online (Sandbox Code Playgroud)
科特林代码
val myIcon =ContextCompat.getDrawable(this@LoginActivity,R.drawable.button)
val filter: ColorFilter = LightingColorFilter(Color.BLACK, Color.BLACK)
myIcon.colorFilter = filter
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3638 次 |
| 最近记录: |