Guy*_*uck 69
根据这个帖子,R.styleable已从Android 1.5及更高版本中删除.
有很多方法可以让样本工作,我发现最简单的是贾斯汀安德森在上面链接的线程中推荐的:
使用以下内容创建名为"resources.xml"的新XML文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Gallery1">
<attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)将XML文件放在res\values目录中(与strings.xml一起)
使用以下内容更新ImageAdapter的构造函数(假设ImageAdapter类在其自己的文件中定义):
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
Run Code Online (Sandbox Code Playgroud)此解决方案基本上将可样式属性定义为应用程序本身的资源,并为其提供在应用程序中工作的必要结构.请注意,如果您只省略两行代码(在a.recycle();之前),应用程序可以正常运行,所有这些代码都会在库中的图像周围设置灰色背景.
Sve*_*ven 12
出现此问题的原因是他们告诉您放入res/values/attrs.xml的资源是:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="HelloGallery">
<attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)
但是你得到了这个适配器,Eclipse无法弄明白,坦白说没有意义:
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(android.R.styleable.Theme);
mGalleryItemBackground = a.getResourceId(
android.R.styleable.Theme_galleryItemBackground, 0);
a.recycle();
}
Run Code Online (Sandbox Code Playgroud)
那是因为你不应该有"android".在资源之前,样式名称是Theme,但实际资源中是HelloGallery,而galleryItemBackground将android放在样式名称和属性之间,如下所示:Theme_android_galleryItemBackground
因此,如果想要ImageAdapter方法使用您提供的资源,您应该像这样重写它:
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground = a.getResourceId(
R.styleable.HelloGallery_android_galleryItemBackground, 0);
a.recycle();
}
Run Code Online (Sandbox Code Playgroud)
对于有关资源的未来问题(R.*无法解析类型错误),请检查/gen/R.java以了解实际命名的资源.
一个稍微容易,当然更MVCish的方式是使用样式系统:
如果你没有一个主题呢,创建styles.xml下res/values.在其中,您应该:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="GalleryItem">
<item name="android:background">?android:attr/galleryItemBackground</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
这将定义我们呼吁一种新的风格GalleryItem和设定任何的风格被应用到的风格属性值的背景资源android:attr/galleryItemBackground(你可以看到很多这种幸福在做的例子frameworks/base/core/res/res/values/themes.xml在Android的源).
然后在ImageView的XML声明中,您可以GalleryItem通过添加简单地应用您的样式style="@style/GalleryItem",例如:
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/icon"
android:scaleType="fitXY"
android:layout_width="136dip"
android:layout_height="88dip"
style="@style/GalleryItem"
/>
Run Code Online (Sandbox Code Playgroud)
这将使您的样式内容远离适配器代码(这很好!)并允许更多通用适配器,无需关心如何可视化您的数据.
我遇到了同样的问题,并且在Google的自定义视图示例代码(PieChart)中找到了
import com.example.android.customviews.R;
Run Code Online (Sandbox Code Playgroud)
当我评论该导入行时,Eclipse将注意到错误:“ R无法解析为变量”。因此,您应该导入与上述类似的软件包声明。例如:
import your.package.name.R;
Run Code Online (Sandbox Code Playgroud)
它为我的其他项目修复了类似的错误
| 归档时间: |
|
| 查看次数: |
46182 次 |
| 最近记录: |