android - Using <include> tag with ?attr/myAttr -
i'm trying include different layouts in view depending on parent theme
.
following idea:
attrs.xml
<attr name="themelayout" format="reference" />
styles.xml
<style name="apptheme" parent="theme.appcompat.light.darkactionbar"> <item name="themelayout">@layout/layout_a</item> </style> <style name="appthemesecond" parent="theme.appcompat.light.darkactionbar"> <item name="themelayout">@layout/layout_b</item> </style>
activity.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".mainactivity"> <include layout="?attr/themelayout" /> </relativelayout>
when run code above i'll following exception:
java.lang.runtimeexception: unable start activity componentinfo{com.my.package/com.my.package.mainactivity}: android.view.inflateexception: must specifiy valid layout reference. layout id ?attr/themelayout not valid. @ android.app.activitythread.performlaunchactivity(activitythread.java:2325) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2387) @ android.app.activitythread.access$800(activitythread.java:151) @ android.app.activitythread$h.handlemessage(activitythread.java:1303) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5254) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:698) caused by: android.view.inflateexception: must specifiy valid layout reference. layout id ?attr/themelayout not valid. @ android.view.layoutinflater.parseinclude(layoutinflater.java:866)
what doing wrong? possible @ all?
note #1: set theme
of mainactivity
android:theme="@style/apptheme"
note #2: in design-view of layout editor works expected. if switch theme, include gets updated properly.
see following screenshots well:
unfortunately not possible idea. have tracked flow of layoutinflater
, requieres layout
attribute typedvalue.type_reference
means ?attr
not allowed. left comment in method explain.
public int getattributeresourcevalue(int idx, int defaultvalue) { int t = nativegetattributedatatype(mparsestate, idx); // note: don't attempt convert other types, because // want count on aapt doing conversion us. if (t == typedvalue.type_reference) { return nativegetattributedata(mparsestate, idx); } return defaultvalue; }
android.content.res.xmlblock.java:385
basically have done nothing wrong -- inflation in preview works differently caused confusion.
Comments
Post a Comment