Goo*_*ofy 4 xml android colors
我有一个将绘制椭圆形的xml,代码如下:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#61118"/>
<stroke android:width="1sp" android:color="#1B434D" />
</shape>
Run Code Online (Sandbox Code Playgroud)
现在我在这里android:color="#61118"我需要从java类传递值,这可能吗?
如果没有,有没有其他办法?
遗憾的是,您无法将参数传递给XML Drawables.
如果您没有太多不同的值,则可以使用a <level-list>并提供不同版本的形状.
然后,您将更改与drawable关联的级别以使用更改颜色Drawable.setLevel(int).
my_drawable.xml
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:maxLevel="0">
<shape android:shape="oval">
<solid android:color="@color/red"/>
<stroke android:width="1sp" android:color="@color/border" />
</shape>
</item>
<item android:maxLevel="1">
<shape android:shape="oval">
<solid android:color="@color/green"/>
<stroke android:width="1sp" android:color="@color/border" />
</shape>
</item>
<item android:maxLevel="2">
<shape android:shape="oval">
<solid android:color="@color/blue"/>
<stroke android:width="1sp" android:color="@color/blue" />
</shape>
</item>
</level-list>
Run Code Online (Sandbox Code Playgroud)
MyActivity.java
// myView is a View (or a subclass of View)
// with background set to R.drawable.my_drawable
myView.getBackground().setLevel(0); // Set color to red
myView.getBackground().setLevel(1); // Set color to green
myView.getBackground().setLevel(2); // Set color to blue
// myImageView is an ImageView with its source
// set to R.drawable.my_drawable
myImageView.setImageLevel(0); // Set color to red
myImageView.setImageLevel(1); // Set color to green
myImageView.setImageLevel(2); // Set color to blue
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2892 次 |
| 最近记录: |