我想從主題和樣式中讀取屬性值,這些屬性值是爲比我運行我的應用程序更新的平臺而設計的。在較舊的平臺上閱讀較新的主題屬性
請不要問爲什麼。如果你知道關於我編寫的庫的任何信息,那麼你應該已經知道我喜歡推平臺的功能:)
我在假設Android樣式編譯時使用的屬性常量是什麼因此理論上應該可以在任何平臺上以某種方式閱讀。這是我觀察到的與我的其他庫中佈局XML沒有任何關係的情況。
這是一個顯示問題的基本測試用例。這應該使用Android 3.0+進行編譯。
<resources>
<style name="Theme.BreakMe">
<item name="android:actionBarStyle">@style/Widget.BreakMe</item>
</style>
<style name="Widget.BreakMe" parent="android:Widget">
<item name="android:padding">20dp</item>
</style>
</resources>
,這裏使用android:actionBarStyle
具體事實是irreleveant。所有應該理解的是它的一個屬性,只有從Android 3.0開始纔可用。
以下是我試圖在Android 3.0之前的平臺上訪問這些值的方式。
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Break Me"
style="?android:attr/actionBarStyle"
/>
和
<declare-styleable name="Whatever">
<item name="datStyle" format="reference" />
</declare-styleable>
<style name="Theme.BreakMe.Take2">
<item name="datStyle">?android:attr/actionBarSize</item>
</style>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Break Me"
style="?attr/datStyle"
/>
和
TypedValue outValue = new TypedValue();
context.getTheme().resolveAttribute(android.R.attr.actionBarStyle, outValue, true);
和
int[] Theme = new int[] { android.R.attr.actionBarSize };
int Theme_actionBarSize = 0;
TypedArray a = context.obtainStyledAttributes(attrs, Theme);
int ref = a.getResourceId(Theme_actionBarSize, 0);
和
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ActionBar, android.R.attr.actionBarStyle, 0);
在這種錯誤的logcat所有這些導致:
E/ResourceType(5618): Style contains key with bad entry: 0x010102ce
的0x010102ce
不變的是這似乎表明該平臺拒絕屬性之前,我甚至能有機會訪問的android.R.attr.actionBarStyle
屬性值的值。
我正在尋找任何其他方式來從主題中讀取像這樣的屬性。我很確定,一旦我獲得了風格參考,我就不會在閱讀它的屬性時遇到麻煩。
有沒有任何可能的方法來做到這一點?
感謝您深入挖掘。 – 2012-01-15 06:20:07
小小的OT,你怎麼知道/認爲尋找ResourceTypes.cpp?我看到很多由@CommonsWare回答的問題,所以我傾向於認爲你是Android專家。 – 2012-04-29 22:47:08
@ radioact1ve:我可能搜索了錯誤消息文本。 – CommonsWare 2012-04-29 23:08:50