use*_*809 0 android android-layout android-xml
我有这样的布局:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/user_color">
<shape android:shape="rectangle">
<solid android:color="#000000" />
</shape>
</item>
<item android:right="25dp">
<shape android:shape="rectangle">
<solid android:color="@color/user_background" />
</shape>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
我怎么称呼形状:
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/shape">
Run Code Online (Sandbox Code Playgroud)
如何以编程方式更改带有id的矩形形状的颜色user_color?
int colorToPaint = getResources().getColor(android.R.color.white);// any color you want
Drawable tempDrawable = getResources().getDrawable(R.drawable.xml_layout);
LayerDrawable bubble = (LayerDrawable) tempDrawable; //cast to root element in xml
GradientDrawable solidColor = (GradientDrawable) bubble.findDrawableByLayerId(R.id.user_color);
solidColor.setColor(colorToPaint);
Run Code Online (Sandbox Code Playgroud)