Error while trying to set custom shape as background of widget
I want to set the background of the standard TextView
in an Android project to a cust开发者_JS百科om shape. The shape is defined as follows:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient android:type="radial"
android:angle="90"
android:startColor="#FF1EFFFF"
android:endColor="#B01E90FF"
android:centerColor="#201E90FF"
android:centerX="0.5"
android:centerY="1.0" />
</shape>
I try to set the custom shape by calling the setBackgroundResource()
method on the textView.
TextView main = (TextView) findViewById(R.id.mainTextView);
main.setBackgroundResource(R.drawable.gradient);
However, when I deploy this project on either a real device or on the Android Emulator, it won't start, and show this error:
ERROR/AndroidRuntime(4369): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.android.gradienttest/org.android.gradienttest.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class <unknown>
Trying to set the drawable by first getting it as a shape results in the same error. By getting it as a shape I mean:
Resources res = getResources();
Shape shape = res.getDrawable(R.drawable.gradient);
But as I said, this results in the same error... I know it gives the error when I try to load the shape, but the problem is I don't know why... So could anybody give me some help on this one? It must be a trivial mistake, as there isn't that much code involved...
It appears that I forgot to add an attribute to my XML file. The attribute was android:gradientRadius="180"
. By adding this attribute the gradient was being drawn correctly. Thanks user639183!
CenterX and centerY are supposed to be integers. This page has a good listing of what all of the parameters will take.
精彩评论