gpt4 book ai didi

android - 在旧平台上阅读新主题属性

转载 作者:IT老高 更新时间:2023-10-28 21:52:58 26 4
gpt4 key购买 nike

我正在尝试从为比我运行我的应用程序更新的平台设计的主题和样式中读取属性值。

请不要问为什么。如果您对我编写的库有所了解,那么您应该已经知道我喜欢插入平台的功能:)

我的操作假设是,编译 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 的事实是无关紧要的。应该理解的是,它是一个仅从 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 的属性值,这似乎表明平台在我有机会之前拒绝了该属性访问它的值。

我正在寻找从主题中读取此类属性的任何其他方式。我相当肯定,一旦我获得了样式引用,我就不会在阅读它的属性时遇到问题。

有没有办法做到这一点?

最佳答案

I am operating under the presumption that when Android styles are compiled the attribute constants are what is used for the keys and therefore should theoretically be able to be read on any platform somehow.

可能,尽管这不是我解释引发您所看到的错误的 C++ 源代码的方式。查看 frameworks/base/libs/utils/ResourceTypes.cpp 中的 ResTable::Theme::applyStyle()

我的解释是,Android 有一个内存中的包表->类型->可能的条目:

numEntries = curPI->types[t].numEntries;

您的条目索引高于已知的最高条目:

if (e >= numEntries) {
LOGE("Style contains key with bad entry: 0x%08x\n", attrRes);
bag++;
continue;
}

对于 android 与其他软件包相比,它们可能会处理这种不同的情况——android 在固件构建时使用已知值(并且您生成的条目索引更高,因为它来自一个较新的平台),非 android 假设任何东西都是有效的。

如果我的猜测是正确的,那么你想要做的就不会奏效。话虽如此,我的 C++ 日子严重在我的后视镜中,所以我可能误解了我所看到的。

关于android - 在旧平台上阅读新主题属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8814109/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com