我有一个drawable,我用作LinearLayout的背景.我想在运行时更改此Shape的颜色.我尝试过使用几种方法..但都没有用.
我遵循了这里描述的方法:http://www.anddev.org/android-2d-3d-graphics-opengl-problems-f55/change-shape-drawable-solid-color-t16798.html
但是有同样的问题......它不会崩溃..但颜色不会改变!
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#00A6C1" />
<corners android:radius="@dimen/square_corners" />
</shape>
Run Code Online (Sandbox Code Playgroud)
代码片段:
GradientDrawable drawable = (GradientDrawable) activity.getResources().getDrawable(R.drawable.blue_square_shape);
int color = ((Application) getApplication()).getColor();
drawable.setColor(color);
block.findViewById(R.id.blockSquare).setBackgroundDrawable(drawable);
findViewById(R.id.blockSquare).postInvalidate();
Run Code Online (Sandbox Code Playgroud)
任何线索?我已经过了一整天谷歌搜索...而且它变得很烦人......
更新:
当我尝试对此形状执行相同操作时:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/shape" android:shape="rectangle">
<gradient android:startColor="#1FBCCF" android:endColor="#06A4C1"
android:angle="270" />
<corners android:topLeftRadius="@dimen/footer_corners"
android:topRightRadius="@dimen/footer_corners" />
</shape>
Run Code Online (Sandbox Code Playgroud)
颜色变成黑色......我想它可以改变...
net*_*ein 42
我现在正在创建一个类似于预编译器的Drawable ..因为我无法将颜色更改为除黑色之外的任何颜色,即使在尝试下面描述的十六进制或之后也是如此.
新代码:
ShapeDrawable footerBackground = new ShapeDrawable();
// The corners are ordered top-left, top-right, bottom-right,
// bottom-left. For each corner, the array contains 2 values, [X_radius,
// Y_radius]
float[] radii = new float[8];
radii[0] = activity.getResources().getDimension(R.dimen.footer_corners);
radii[1] = activity.getResources().getDimension(R.dimen.footer_corners);
radii[2] = activity.getResources().getDimension(R.dimen.footer_corners);
radii[3] = activity.getResources().getDimension(R.dimen.footer_corners);
footerBackground.setShape(new RoundRectShape(radii, null, null));
int color = ((Application) activity.getApplication()).getColor();
footerBackground.getPaint().setColor(color);
views.setBackgroundDrawable(footerBackground);
Run Code Online (Sandbox Code Playgroud)
无论如何这是一个修复..第一个问题的解决方案是我真正想要的!当然,我会感激任何帮助!
Sau*_*nda 34
看看类似的东西是否适合你:
TextView tv2 = (TextView) rl.findViewById(R.id.toggle_indicator);
/* Refer to http://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html#mutate()
to understand why we need to mutate the GradientDrawable*/
GradientDrawable sd = (GradientDrawable) tv2.getBackground().mutate();
sd.setColor(0xff999999);
sd.invalidateSelf();
Run Code Online (Sandbox Code Playgroud)
在我的例子中,我有一个TextView,它有一个ShapeDrawable作为背景.我想改变它的颜色,并设法使这项工作.令人费解的是,tv2.getBackground()返回一个GradientDrawable而不是一个ShapeDrawable - 这也在其他地方报道过.
编辑:关于颜色,尝试设置alpha值为0xff.如果您注意到,即使在上面的代码中,setColor()函数也会从常规RGB十六进制值中获取额外的十六进制值.这适用于Alpha/Opacity.如果将其设置为0x00,Drawable将具有黑色,与RGB无关(假设您的背景颜色为黑色).0x00是一个完全透明的对象,0xff是一个完全不透明的对象.
小智 29
GradientDrawable background = (GradientDrawable) titleTextView.getBackground();
background.setColor(getResources().getColor(R.color.some_color));
Run Code Online (Sandbox Code Playgroud)
setColor方法正确地请求对元素进行重绘(如果在xml中使用了<shape>元素,它将始终成为GradientDrawable)
小智 8
R.drawable.library_cirecle
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/outerRectangle">
<shape android:shape="oval" >
<solid android:color="#FCD366" />
<stroke
android:width="1dp"
android:color="@android:color/darker_gray" />
</shape>
</item>
Run Code Online (Sandbox Code Playgroud)
在代码中更改颜色
Drawable tempDrawable = getResources().getDrawable(R.drawable.library_cirecle);
LayerDrawable bubble = (LayerDrawable) tempDrawable; (cast to root element in xml)
GradientDrawable solidColor = (GradientDrawable) bubble.findDrawableByLayerId(R.id.outerRectangle);
solidColor.setColor(colorToPaint);
imageView.setImageDrawable(tempDrawable);
Run Code Online (Sandbox Code Playgroud)
有一个更简单的方法:
ShapeDrawable drawable = new ShapeDrawable();
drawable.getPaint().setColor(getResources().getColor(R.color.blue));
getActionBar().setBackgroundDrawable(drawable);
Run Code Online (Sandbox Code Playgroud)