2009-11-11 35 views

回答

69

this thread,R.styleable已经在Android 1.5或更高去除。

有许多的方式来获得样品的工作,我发现最简单的是在连接到上面的线索推荐贾斯汀·安德森:

  1. 创建一个名为“资源新的XML文件。 XML”,内容如下:

    <?xml version="1.0" encoding="utf-8"?> 
    <resources> 
        <declare-styleable name="Gallery1"> 
         <attr name="android:galleryItemBackground" /> 
        </declare-styleable> 
    </resources> 
    
  2. 放入水库XML文件\目录值(旁边的strings.xml)

  3. 最多日期与下面的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(); 
    } 
    

该解决方案的设置样式属性基本上定义为应用程序本身的资源,并赋予它的必要的结构在应用程序中工作。请注意,应用程序可以运行正常,如果你只是忽略的两行代码(前a.recycle();),所有这些代码也被设置在图库中的图像周围灰色背景。

+4

我想几千人一定有过这样的问题。 – Ralphleon 2009-11-11 22:51:45

+1

是啊,我只好掏了一会儿后,我发现一个简单的和可行的解决方案,所以我想我会在这里发布的SO,使其更容易为乡亲找到。 – 2009-11-12 00:12:51

+1

谢谢你。我希望他们能更新教程! 顺便说一句 - 我需要命名资源子元素declare-styleable,即styleable不可风化。 – 2009-12-23 12:19:13

11

这样做的原因问题是,他们告诉你投入RES /价值/ attrs.xml的资源:

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

但你得到这个适配器,它的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(); 
} 

这是因为你不应该有“机器人”。在资源之前,风格化的名字是这里的主题,但是在实际资源中的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(); 
} 

有关资源未来的问题(R. *解决不了类型错误),检查/gen/R.java的资源实际上被命名什么。

+0

@YumYumYum它的作品,它是正确的答案。 – 2016-01-05 16:45:50

1

有有选择的回答有点错误,而不是与设置样式

风格化的它应该是这样的:

<declare-styleable name="Gallery1"> 
    <attr name="android:galleryItemBackground" /> 
</declare-styleable> 
+0

否 - 没有工作,不正确的答案。 – YumYumYum 2013-07-25 11:31:25

5

一个稍微容易些,当然更MVCish的方法是使用样式系统:

如果你没有一个主题呢,创建一个styles.xmlres/values。在这里面,你应该有:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="GalleryItem"> 
     <item name="android:background">?android:attr/galleryItemBackground</item> 
    </style> 
</resources> 

这将定义我们呼吁GalleryItem和设定任何的风格被应用到后台资源的新风格,样式属性android:attr/galleryItemBackground的值(你可以看到在Android的源代码中的frameworks/base/core/res/res/values/themes.xml中有很多这样的例子)。

然后在一个ImageView的一个XML声明,你可以简单地通过增加style="@style/GalleryItem"应用您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" 
/> 

这将让你的风格的东西出你的适配器代码(这是很好的!)并允许更多的通用适配器不需要关心如何将数据可视化。

+0

尽管如何将ImageView挂接到画廊?该库没有任何在XML中声明的内部元素。 – 2011-02-22 01:48:49

+0

是的,画廊通过Adapters提供意见。您将需要创建一个适配器,从xml扩充ImageView。有关示例,请参阅ApiDemos/src/com/example/android/apis/view/Gallery2.java。 – 2011-02-22 21:42:10

+0

是的,这种方法更简洁,即使是使用xml声明的ImageView,而不是代码创建的 – superjos 2011-06-15 21:09:01

3

设置样式不支持http://developer.android.com/sdk/RELEASENOTES.html

的android.R.styleable类及其字段从公众API中删除,以更好地确保应用程序向前兼容。 android.R.styleable中声明的常量是特定于平台的,并且可以在各个版本之间进行任意更改,因此不适合应用程序使用。您仍然可以从资源或代码访问平台的可修改属性。要做到这一点,在申报项目的res/values/R.attrs文件中使用<declare-styleable>自定义资源元素,那么内声明的属性。有关示例,请参见<sdk>/samples/ApiDemos/res/values/attrs.xml。有关自定义资源的更多信息,请参阅自定义布局资源。需要注意的是android.R.styleable文档在SDK还提供了,但只是作为各种要素平台的设置样式属性的参考。

1

我什么都试过,但没有运气。生成的R.java显示类风格的类,但汇编显示“Stylable not found”。我在R之前刚刚添加的软件包名称,变更后,一切现在工作正常...

所以,如果你的包名是com.example.test,然后修改下面的代码...

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

}

TO

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

我有同样的问题,我在谷歌

的自定义视图示例代码(饼图)发现
import com.example.android.customviews.R; 

当我评论说,进口线,Eclipse会注意到错误:“R不能被解析为一个变量”。所以你应该导入上面的包类似的声明。例如:

import your.package.name.R; 

它修复类似的错误了我的其他项目