我有一个非常简单的文本视图:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textSize="20sp"
android:textColor="@style/basic_text_color"
android:layout_gravity="center"
android:id="@+id/toolbar_title" />
@ style / basic_text_color是我想在应用中重复使用很多时间的颜色,所以我在app_colors.xml
文件夹下创建了一个values
:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Text colors style-->
<style name="basic_text_color" parent="@android:style/TextAppearance">
<item name="android:textColor">#ff932e</item>
</style>
<!-- some other color styles-->
</resources>
应用程序编译成功,但随后执行时它将挂起并在LogCat中提示跟随错误:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aaa/com.example.aaa.activity.MainActivity}: android.view.InflateException: Binary XML file line #17: Error inflating class TextView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
at android.app.ActivityThread.access$600(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class TextView
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
at com.example.aaa.activity.MainActivity.onCreate(MainActivity.java:54)
at android.app.Activity.performCreate(Activity.java:5206)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
... 11 more
Caused by: android.content.res.Resources$NotFoundException: Resource is not a ColorStateList (color or path): TypedValue{t=0x1/d=0x7f0b0124 a=-1 r=0x7f0b0124}
at android.content.res.Resources.loadColorStateList(Resources.java:2068)
at android.content.res.TypedArray.getColorStateList(TypedArray.java:342)
at android.widget.TextView.<init>(TextView.java:896)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:44)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:40)
at android.support.v7.internal.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103)
at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:806)
at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:836)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:675)
... 22 more
所以看起来这里的错误是&#34;资源不是ColorStateList(颜色或路径):TypedValue {t = 0x1 / d = 0x7f0b0124 a = -1 r = 0x7f0b0124}&#34; ...
我是否以错误的方式设置了字体颜色?或者其他任何问题都出错了?谢谢!
答案 0 :(得分:2)
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="basic_text_color">#ff932e</color>
</resources>
布局
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textSize="20sp"
android:textColor="@colors/basic_text_color"
android:layout_gravity="center"
android:id="@+id/toolbar_title" />
如果要更改textColor,则必须创建一个颜色资源文件,因为在您的问题中,您尝试将颜色设置为样式。
答案 1 :(得分:1)
您可能打算使用:
style="@style/basic_text_color"
答案 2 :(得分:0)
您应该使用color属性而不是样式1。
<color name="basic_text_color">#YOUR COLOR GOES HERE</color>
答案 3 :(得分:0)
如果要从Java设置颜色,可以使用以下内容:
myView.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.grey_300)));