Android: theme does not work in layout file

According to this :

Elements in XML layout definitions can indicate an android theme: an attribute that refers to a theme resource. This attribute changes the theme for the element and any child elements, which is useful for changing the color palettes of themes in a certain part of the interface.

This means that we can specify android:theme in the layout files. And also, if we look at how the Toolbar used:

 <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/green" android:minHeight="?attr/actionBarSize" app:theme="@style/ThemeOverlay.AppCompat.ActionBar" /> 

It is understood that theme support has been added.

Now I am trying to do this:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Test" android:theme="@style/TextViewTheme" /> </LinearLayout> 

Where is TextViewTheme :

 <style name="TextViewTheme" parent="TextAppearance.AppCompat"> <item name="android:textColor">#ff0000</item> </style> 

This looks great in the design editor, but at runtime I get this exception:

 12-05 15:56:40.414 32501-32501/com.my.app E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.my.app, PID: 32501 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.app/com.my.app.TabbedActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class android.widget.TextView at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread.access$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class android.widget.TextView at android.view.LayoutInflater.createView(LayoutInflater.java:633) at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55) at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741) at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) at android.view.LayoutInflater.inflate(LayoutInflater.java:504) at android.view.LayoutInflater.inflate(LayoutInflater.java:414) at com.my.app.ui.DrawerFragment.onCreateView(DrawerFragment.java:20) at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:920) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104) at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1206) at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2159) at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:297) at android.support.v7.app.ActionBarActivity.onCreateView(ActionBarActivity.java:547) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727) at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) at android.view.LayoutInflater.inflate(LayoutInflater.java:504) at android.view.LayoutInflater.inflate(LayoutInflater.java:414) at android.view.LayoutInflater.inflate(LayoutInflater.java:365) at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228) at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102) at com.my.app.TabbedActivity.onCreate(TabbedActivity.java:40) at android.app.Activity.performCreate(Activity.java:5933) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)            at android.app.ActivityThread.access$800(ActivityThread.java:144)            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)            at android.os.Handler.dispatchMessage(Handler.java:102)            at android.os.Looper.loop(Looper.java:135)            at android.app.ActivityThread.main(ActivityThread.java:5221)            at java.lang.reflect.Method.invoke(Native Method)            at java.lang.reflect.Method.invoke(Method.java:372)            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.newInstance(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:288) at android.view.LayoutInflater.createView(LayoutInflater.java:607)            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)            at com.my.app.ui.DrawerFragment.onCreateView(DrawerFragment.java:20)            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:920)            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)            at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1206)            at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2159)            at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:297)            at android.support.v7.app.ActionBarActivity.onCreateView(ActionBarActivity.java:547)            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)            at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)            at com.my.app.TabbedActivity.onCreate(TabbedActivity.java:40)            at android.app.Activity.performCreate(Activity.java:5933)            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)            at android.app.ActivityThread.access$800(ActivityThread.java:144)            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)            at android.os.Handler.dispatchMessage(Handler.java:102)            at android.os.Looper.loop(Looper.java:135)            at android.app.ActivityThread.main(ActivityThread.java:5221)            at java.lang.reflect.Method.invoke(Native Method)            at java.lang.reflect.Method.invoke(Method.java:372)            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) Caused by: java.lang.RuntimeException: Failed to resolve attribute at index 24 at android.content.res.TypedArray.getColor(TypedArray.java:401) at android.widget.TextView.<init>(TextView.java:692) at android.widget.TextView.<init>(TextView.java:629) at android.widget.TextView.<init>(TextView.java:625)            at java.lang.reflect.Constructor.newInstance(Native Method)            at java.lang.reflect.Constructor.newInstance(Constructor.java:288)            at android.view.LayoutInflater.createView(LayoutInflater.java:607)            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)            at com.my.app.ui.DrawerFragment.onCreateView(DrawerFragment.java:20)            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:920)            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)            at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1206)            at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2159)            at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:297)            at android.support.v7.app.ActionBarActivity.onCreateView(ActionBarActivity.java:547)            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)            at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)            at com.my.app.TabbedActivity.onCreate(TabbedActivity.java:40)            at android.app.Activity.performCreate(Activity.java:5933)            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)            at android.app.ActivityThread.access$800(ActivityThread.java:144)            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)            at android.os.Handler.dispatchMessage(Handler.java:102)            at android.os.Looper.loop(Looper.java:135)            at android.app.ActivityThread.main(ActivityThread.java:5221)            at java.lang.reflect.Method.invoke(Native Method)            at java.lang.reflect.Method.invoke(Method.java:372)            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

In addition, I looked at the source code of the Google IO application and could not find the android:theme files inside the layout files. I didn’t understand something?

+5
source share
1 answer

In the case of TextView , not android:theme="@style/TextViewTheme" , you should use style="@style/TextViewTheme" .

This is because (from http://developer.android.com/guide/topics/ui/themes.html ):

A theme is a style that applies to the entire Office or application, and not to a single view (as in the example above). When a style is applied as a theme, each view in an Activity or application will apply each property of the style that it supports. For example, you can apply the same CodeFont style as the theme for the Activity, and then all the text inside this Activity will have a green monospaced font.

I think this replacement can also solve your problem.

If you want all TextView look the same, try including their style in AppTheme as follows:

 <!-- Application theme. --> <style name="AppTheme" parent="AppBaseTheme"> <item name="android:textViewStyle">@style/TextViewTheme</item> </style> 
+8
source

Source: https://habr.com/ru/post/1208418/


All Articles