Sta*_*tam 18 android shape android-drawable layerdrawable
我试图以编程方式更改selectable_kachel_shape的颜色.这是xml文件:
kachel_ticked_style.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape
android:id="@+id/selectable_kachel_shape"
android:shape="rectangle" >
<stroke
android:width="5dp"
android:color="@color/headrbar_color" />
</shape>
</item>
<item>
<rotate
android:fromDegrees="45"
android:pivotX="120%"
android:pivotY="100%"
android:toDegrees="45" >
<shape android:shape="line" >
<stroke
android:width="40dp"
android:color="@color/headrbar_color" />
</shape>
</rotate>
</item>
<item
android:right="5dp"
android:top="5dp">
<bitmap
android:gravity="top|right"
android:src="@drawable/selectable_tiles_check" />
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
我在片段内调用以下代码
LayerDrawable layers = (LayerDrawable) this.getActivity().getResources().getDrawable(R.drawable.kachel_ticked_style);
GradientDrawable shape = (GradientDrawable) (layers.findDrawableByLayerId(R.id.selectable_kachel_shape));
shape.setColor(this.getActivity().getResources().getColor(android.R.color.background_dark);
Run Code Online (Sandbox Code Playgroud)
1.为什么我在shape.setColor中设置NullPointerException ?
2.如何以编程方式更改图层内部的形状内的颜色?
Sta*_*tam 24
好的,我找到了答案,我只需将形状的id放在项目中,而不是形状为 kachel_ticked_style.xml:
<item
android:id="@+id/selectable_kachel_shape">
<shape
android:shape="rectangle" >
<stroke
android:width="5dp"
android:color="@color/headrbar_color" />
</shape>
</item>
Run Code Online (Sandbox Code Playgroud)
然后你可以改变调用shape.setColor的形状的颜色或调用shape.setStroke(strokeWidth,strokeColor)的笔触的颜色.
| 归档时间: |
|
| 查看次数: |
11316 次 |
| 最近记录: |