我有两个项目,分别代表层列表xlm中的两个形状。第二个形状具有渐变属性。在第二项中,我要以编程方式更改形状颜色以及渐变的开始和结束颜色。
这是我的状态列表
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="@drawable/shapecopmainbutton" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="@drawable/shapecopmainbuttonpress" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="@drawable/shapecopmainbutton" />
<item
android:state_enabled="true"
android:drawable="@drawable/shapecopmainbutton" />
</selector>
这是我的图层列表:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<size
android:width="@dimen/layoutwidthMaincopbutton"
android:height="@dimen/HeightMaincopbutton" />
<solid android:color="@color/black"/>
</shape>
</item>
<item
android:id="@+id/gradientDrawble"
android:left="4dp"
android:right="1dp"
android:top="0dp"
android:bottom="5.5dp"
>
<shape
android:id="@+id/gradientDrawbles"
android:shape="ring"
android:thickness="@dimen/thicknessMaincopbutton"
android:innerRadius="@dimen/innerRadiusMaincopbutton"
android:useLevel="false">
<solid android:color="@color/GradientEnd" />
<gradient
android:id="@+id/gradientDrawbleg"
android:type="radial"
android:gradientRadius="30%p"
android:startColor="@color/GradientCenter"
android:endColor="@color/GradientEnd"
android:centerX="0.2"
android:centerY="0.2"
/>
</shape>
</item>
这是我到目前为止拥有的Java:
StateListDrawable stateListDrawable=(StateListDrawable) ContextCompat.getDrawable(context,R
.drawable.copbuttonmaineffect);
LayerDrawable layerDrawable = (LayerDrawable) stateListDrawable.getCurrent();
int colors[] = { R.color.GradientCenter, R.color.GradientEndl };
GradientDrawable gradientDrawable = (GradientDrawable) layerDrawable.findDrawableByLayerId(R.id.
gradientDrawbleRing);
gradientDrawable.setColors(colors);
int color2=ContextCompat.getColor(context, R.color.GradientEndl);
gradientDrawable.setColor(color2);
g.getCurrentbutton().setBackground(gradientDrawable);
当我运行这个 戒指的颜色会改变 渐变的开始和结束颜色会根据需要进行更改。 但是椭圆形的颜色从黑色变为白色。 为什么会这样呢?以及如何防止它这样做。 谢谢。