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

估计没多少人会用到,权当自己的笔记。
以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"
        />
这样就可以了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值