5

Nach this:android: Thema arbeitet nicht in Layout-Datei

Elemente in XML-Layoutdefinitionen die Androiden angeben: Thema Attribut, das ein Thema Ressource verweist. Dieses Attribut ändert das Design für das Element und alle untergeordneten Elemente, was für das Ändern von Farbpaletten in einem bestimmten Abschnitt einer Schnittstelle nützlich ist.

Was bedeutet, dass wir android:theme in Layout-Dateien angeben können. Und auch wenn wir die Art und Weise betrachten Toolbar verwendet:

<?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" /> 

Es ist klar, dass theme Unterstützung hinzugefügt wurde.

Jetzt versuche ich dies:

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

Wo TextViewTheme ist:

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

Dies scheint perfekt in Design-Editor zu sehen, aber zur Laufzeit ich diese Ausnahme bin immer:

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) 

Außerdem schaute ich durch Google IO app source code und konnte keine finden android:theme innerhalb Layout-Dateien. Habe ich etwas falsch verstanden?

+0

Ich stimme der Dokumentation ist nicht klar. 'android: theme' wird nicht für alles AFAIK unterstützt, aber' style' funktioniert immer noch. – shkschneider

+1

'android: theme' wird überall in API 21+ unterstützt, aber zur Zeit unterstützt nur die Toolbar das' theme' Attribut in appcompat für API <21. – alanv

Antwort

8

Bei TextView anstelle von android:theme="@style/TextViewTheme" sollten Sie style="@style/TextViewTheme" verwenden.

, die wegen (von http://developer.android.com/guide/topics/ui/themes.html) ist:

Ein Thema ist ein Stil auf eine gesamte Aktivität oder Anwendung angewendet wird, anstatt eine einzelne Ansicht (wie im Beispiel oben). Wenn ein Stil als Thema angewendet wird, wendet jede Ansicht in der Aktivität oder Anwendung jede von ihm unterstützte Stileigenschaft an. Sie können beispielsweise denselben CodeFont-Stil wie ein Design für eine Aktivität anwenden, und dann wird der gesamte Text innerhalb dieser Aktivität mit einer grünen Monospace-Schriftart versehen.

Ich denke, dass dieser Ersatz auch Ihr Problem lösen kann.

Wenn Sie möchten, dass alle TextView s gleich aussieht, versuchen, ihren Stil in AppTheme wie folgt umfassen:

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

Sie Code funktioniert zwar, aber ich bin mir sicher, dass das OP den Stil kennt 'tag. Er hat Recht mit der irreführenden Dokumentation. – shkschneider