您的位置:首页 > 移动开发 > Android开发

Android开发——如何在第三方应用中调用系统未开放的主题和样式

2013-03-15 17:12 831 查看
估计没多少人会用到,权当自己的笔记。

以android 4.0代码为例,如果写了一个Button,想要使用某种样式(系统或自定义的都可以),可以在.xml布局文件中如下定义。

<Button

        ......

        style="@android:style/Widget.DeviceDefault.Button"

        />

上述代码引用了Android系统默认的Button样式。这样是可以的,因为 Widget.DeviceDefault.Button 这个样式是开放的。

所谓开放,是指该样式在系统中经过注册,可以被第三方应用调用,直观的表现就是可以编译通过。

具体的,注册是 /frameworks/base/core/res/res/values/public.xml 在完成的,内容如下:

<resources>

  <!-- We don't want to publish private symbols in android.R as part of the

       SDK.  Instead, put them here. -->

  <private-symbols package="com.android.internal" />

  ......

  <public type="style" name="Widget.DeviceDefault.Button" id="0x01030141" />

  <public type="style" name="Widget.DeviceDefault.Button.Small" id="0x01030142" />

  <public type="style" name="Widget.DeviceDefault.Button.Inset" id="0x01030143" />

  <public type="style" name="Widget.DeviceDefault.Button.Toggle" id="0x01030144" />

  <public type="style" name="Widget.DeviceDefault.Button.Borderless.Small" id="0x01030145" />

  ......

</resources>

然而,并且所有样式和主题都是公开的;没有在public.xml中定义的,就不能用上面的方法调用。

<Button

        ......

        style="@android:style/Widget.DeviceDefault.Button.AlertDialog"

        />

如果使用上述代码,则编译会报错: Error: Resource is not public. (at 'style' with value '@android:style/Widget.DeviceDefault.Button.AlertDialog').

解决方法很简单,在“@”后面加一个“*”,改为:

<Button

        ......

        style="@*android:style/Widget.DeviceDefault.Button.AlertDialog"

        />

这样就可以了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  style theme Android
相关文章推荐