相关疑难解决方法(0)

在较旧的平台上阅读较新的主题属性

我试图从主题和样式中读取属性值,这些属性值是为比运行我的应用程序更新的平台而设计的.

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

我的假设是,在编译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>
Run Code Online (Sandbox Code Playgroud)

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"
    />
Run Code Online (Sandbox Code Playgroud)

<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"
    />
Run Code Online (Sandbox Code Playgroud)

TypedValue outValue = new TypedValue();
context.getTheme().resolveAttribute(android.R.attr.actionBarStyle, outValue, true);
Run Code Online (Sandbox Code Playgroud)

int[] Theme = new int[] { android.R.attr.actionBarSize };
int Theme_actionBarSize = 0;
TypedArray a = context.obtainStyledAttributes(attrs, Theme);
int …
Run Code Online (Sandbox Code Playgroud)

android android-theme

30
推荐指数
1
解决办法
5045
查看次数

标签 统计

android ×1

android-theme ×1