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
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?
source share