在android中以编程方式更改可绘制颜色
我有以下平行四边形:在android中以编程方式更改可绘制颜色,android,xml,drawable,Android,Xml,Drawable,我有以下平行四边形: <vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="200dp" android:height="50dp" android:viewportWidth="200" android:viewportHeight="50"> <path android:fillColor="#000000" and
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="200dp"
android:height="50dp"
android:viewportWidth="200"
android:viewportHeight="50">
<path android:fillColor="#000000"
android:pathData="M 200 0 L 20 0 L 0 50 L 180 50 L 200 0" />
</vector>
无法在运行时更改单个路径的颜色。但是,您可以更改整个向量的颜色。您不能在运行时更改单个路径的颜色。但是,您可以更改整个向量的颜色。尝试使用setBackgroundTint而不是颜色。这将改变背景的来源。尝试使用setBackgroundTint而不是颜色。这将改变背景的资源。 Drawable backgroundDrawable=DrawableCompat.wrapview.getBackgroundDrawable.mutate; DrawableCompat.setTintbackgroundDrawable,颜色; 你可以用这个 Drawable backgroundDrawable=DrawableCompat.wrapview.getBackgroundDrawable.mutate; DrawableCompat.setTintbackgroundDrawable,颜色;
你可以用这个 您是否尝试过使用xml选择器drawable进行触摸?顺便说一句,如果您只想更改形状,您需要一个白色的新drawablecolor@cricket_007你的意思是我创建了一个新的并准确地将其放置在触摸形状上?有没有办法分别为每个视图设置颜色?您是否尝试过使用xml选择器drawable进行触摸?顺便说一句,如果您只想更改形状,您需要一个白色的新drawablecolor@cricket_007你的意思是我创建了一个新的并准确地将其放置在触摸形状上?有没有办法分别设置每个视图的颜色?我有64个这样的形状,只想更改一个,而不是全部。可能吗?因为我认为改变整个矢量颜色会改变我所有的64个形状。我有64个这样的形状,我想改变唯一一个被触摸的形状,而不是所有的形状。可能吗?因为我认为改变整个矢量颜色会改变我所有的64个形状。我使用了getBackgroundDrawable,而不是getBackgroundDrawable,而且效果很好。非常感谢,但我还有一个问题…我使平行四边形具有边框透明,调用测试函数只是更改边框颜色,但当我为形状设置不透明的颜色时,测试函数将更改其填充颜色。我该怎么办呢?我用getBackgroundDrawable代替了getBackgroundDrawable,它成功了。非常感谢,但我还有一个问题…我使平行四边形具有边框透明,调用测试函数只是更改边框颜色,但当我为形状设置不透明的颜色时,测试函数将更改其填充颜色。我该怎么办?
<View
android:layout_width="66dp"
android:layout_height="26dp"
android:layout_centerInParent="true"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="112dp"
android:background="@drawable/shape"
android:onClick="test"
android:tag="p112"
app:layout_constraintHorizontal_bias="0.615"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
public void test(View view){
view.setBackgroundColor(Color.WHITE);
}