这是三角形的 xml:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/shape_id">
<rotate
android:fromDegrees="45"
android:toDegrees="45"
android:pivotX="-40%"
android:pivotY="87%" >
<shape android:shape="rectangle" >
<stroke android:width="10dp"/>
</shape>
</rotate>
</item>
</layer-list>
这是文本视图的背景
<TextView
android:id="@+id/headlineSelect_TXT2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
android:background="@drawable/category_triangle_shape1"
android:visibility="invisible" />
我想以编程方式改变形状的颜色。我尝试了这个,但出现了空指针异常
LayerDrawable bgDrawable = (LayerDrawable) getActivity()
.getResources()
.getDrawable(R.drawable.category_triangle_shape1);
final GradientDrawable shape = (GradientDrawable) bgDrawable
.findDrawableByLayerId(R.id.shape_id);
shape.setStroke(10,Color.GREEN);
我怎样才能做到这一点?感谢帮助。
这有点棘手,涉及很多演员:
TextView view = (TextView) findViewById( R.id.my_text_view );
// Get the drawable from the view, if you're using an imageview src
// element you'll go with view.getDrawable()
LayerDrawable layers = (LayerDrawable) view.getBackground();
// Now get your shape by selecting the id
RotateDrawable rotate = (RotateDrawable) layers.findDrawableByLayerId( R.id.shape_id );
// Finally you can access the underlying shape
GradientDrawable drawable = (GradientDrawable) rotate.getDrawable();
// ... and do you fancy things
drawable.setColor( ... );
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)