R.styleable无法解决,为什么?

Lee*_*eem 15 android android-widget android-manifest android-emulator android-layout

我有一个位于direcotry values /下的resources.xml文件,那就是

/values/resources.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="TheMissingTabWidget"> 
        <attr name="android:divider" /> 
    </declare-styleable> 
</resources>
Run Code Online (Sandbox Code Playgroud)

在我的java代码中,当我尝试访问此资源时R.styleable.TheMissingTabWidget,eclipse抱怨无法解析styleable或者不是字段.为什么?为什么我无法访问此资源?(我正在使用android 2.1更新).

小智 17

plz制作这样的values/attrs.xml资源

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="**com.admob.android.ads.AdView**"><--- where u want to use
       <attr name="backgroundColor" format="color" />
       <attr name="TextColor" format="color" />
       <attr name="keywords" format="string" />
       <attr name="refreshInterval" format="integer" />
    </declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)

  • 您的代码中的通配符是什么? (3认同)

xev*_*ent 15

根据SDK发行说明,

android.R.styleable类及其字段已从公共API中删除,以更好地确保应用程序的前向兼容性.在android.R.styleable中声明的常量是特定于平台的,并且可以在不同版本之间进行任意更改,因此不适合应用程序使用.您仍然可以从资源或代码访问平台的可设置样式属性.为此,请在项目的res/values/R.attrs文件中使用a声明自定义资源元素,然后在其中声明属性.有关示例,请参阅"sdk"/samples/ApiDemos/res/values/attrs.xml.有关自定义资源的详细信息,请参阅自定义布局资源.请注意,SDK中仍提供了android.R.styleable文档,但仅作为平台各种元素的可设置属性的参考.

查看ApiDemos代码和文件res/values/attrs.xml

  • 这并没有解释你如何将 `R.styleable` 放入 **attrs** 文件中。 (3认同)

m_k*_*kis 8

在我的情况下,我无意中完成了import android.R而不是import com.<mypackage>.R.

替换<mypackage>为你的包名(或者只是删除当前的导入,让 Android Studio 做剩下的事情)。

  • 这就是拯救我的答案。它应该比其他答案有更多的赞成票,其他答案的赞成票超过 15 票,而这甚至不是答案。 (2认同)

Ish*_*ndo 5

您可以像这样访问您的包级别样式

<yourpackagename>.R.styleable.name
Run Code Online (Sandbox Code Playgroud)