如何改变从code`固color`?code、color

2023-09-04 03:56:56 作者:归故里

我有一个图形定义的XML 。现在我需要改变纯色从code,请你指教怎么样?

 < XML版本=1.0编码=UTF-8&GT?;
<形状的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:形状=矩形机器人:填充=10dp>
    [固体机器人:颜色=#000000/>
    <角落
        机器人:bottomRightRadius =8DP
        机器人:bottomLeftRadius =8DP
        机器人:topLeftRadius =0dp
        机器人:topRightRadius =0dp/>
< /形状>
 

解决方案

我用这个在我的code

  GradientDrawable myGrad =(GradientDrawable)rectangle.getBackground();
myGrad.setColor(Color.BLACK);
 

希望这有助于:)

编辑:代替 ShapeDrawable

GradientDrawable 修改vscode代码主体颜色

I have a Shape defined in xml. Now I need to change a solid color from the code, could you please advice how?

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" android:padding="10dp">
    <solid android:color="#000000"/>
    <corners
        android:bottomRightRadius="8dp"
        android:bottomLeftRadius="8dp"
        android:topLeftRadius="0dp"
        android:topRightRadius="0dp"/>
</shape>

解决方案

I use this in my code

GradientDrawable myGrad = (GradientDrawable)rectangle.getBackground();
myGrad.setColor(Color.BLACK);

Hope this helps :)

EDIT: for GradientDrawable instead of ShapeDrawable

 
精彩推荐
图片推荐