Android 你好,图库教程——“R.styleable 无法解析" [英] Android Hello, Gallery tutorial -- "R.styleable cannot be resolved"

查看:45
本文介绍了Android 你好,图库教程——“R.styleable 无法解析"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在处理 Hello, Gallery 教程/示例应用程序时,在 以下之后根据网站上的说明,Eclipse 报告无法解析 R.styleable.

When working on the Hello, Gallery tutorial/sample app, after following the instructions on the site, Eclipse reported that R.styleable cannot be resolved.

此错误的原因是什么,如何修复或解决?

What is the reason for this error, and how can it be fixed or worked around?

推荐答案

Per this thread,R.styleable 已从 android 1.5 及更高版本中删除.

Per this thread, R.styleable has been removed from android 1.5 and higher.

有多种方法可以使示例工作,我发现最简单的方法是 Justin Anderson 在上面链接的线程中推荐的:

There are a number of ways to get the sample to work, the simplest that I found was recommended by Justin Anderson in the thread linked to above:

  1. 创建一个名为resources.xml"的新 XML 文件,内容如下:

  1. Create a new XML file called "resources.xml" with the following content:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="Gallery1"> 
        <attr name="android:galleryItemBackground" /> 
    </declare-styleable> 
</resources>

  • 将 XML 文件放在 resvalues 目录中(与 strings.xml 一起)

  • Place the XML file in the resvalues directory (alongside strings.xml)

    使用以下内容更新 ImageAdapter 的构造函数(假设 ImageAdapter 类在其自己的文件中定义):

    Update the constructor for your ImageAdapter with the following (assuming the ImageAdapter class is defined in its own file):

    public ImageAdapter(Context c) {
        mContext = c;
        TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);
        mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
        a.recycle();
    }
    

  • 这个解决方案基本上将 styleable 属性定义为应用程序本身的资源,并为其提供在应用程序中工作的必要结构.请注意,如果您省略两行代码(在 a.recycle(); 之前),应用程序可以正常运行,所有这些代码所做的就是在图库中的图像周围设置灰色背景.

    This solution basically defines the styleable attribute as a resource of the app itself and gives it the necessary structure to work in the app. Note that the app can run fine if you just omit the two lines of code (prior to a.recycle();), all this code does is set a grey background around the images in the Gallery.

    这篇关于Android 你好,图库教程——“R.styleable 无法解析"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆