获取动态风格属性 [英] Getting style attributes dynamically

查看:122
本文介绍了获取动态风格属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法来动态获取样式属性?

Is there a way to dynamically get styled attributes?

例如,要找到一个资源ID,我可以使用:

For instance, to find a resource id, I can use:

context.getResources().getIdentifier("idtofind", "id", context.getPackageName());

有没有类似这样的东西得到样式属性?

Is there something similar to this to get styled attributes?

一个小背景...我有其中有一个自定义的滑动抽屉库。它在项目的/values​​/attrs.xml已经定制风格的属性。

A little background...I have a library which has a custom sliding drawer. It has custom styled attributes in the project's /values/attrs.xml.

我把它砸进去了GLE的用户界面,但它并没有显示出来,因为它找不到把手和内容的ID。该生产线是这样的:

I got it to drop into the UI in the GLE, but it doesn't show up, because it can't find the ids for the handle and content. The line is this:

TypedArray a = context.obtainStyledAttributes( attrs, R.styleable.DrawerSlider, defStyle, 0 );

我不知道如何实现上述目标,作为设置样式是一个int数组,这样则getIdentifier()将无法工作。

I'm not sure how to achieve the above, as the styleable is an int array, so getIdentifier() won't work.

编辑:

好吧,我已经能够得到的东西在code做工精细了,但仍然有与GLE问题。我会尝试更多的现在澄清了一下。就像我说的,这是一个库,它是免费的。

Ok, I've been able to get things to work in code fine now, but still having problems with the GLE. I'll try to clarify a bit more now. Like I said, this is for a library, which is freely available.

经过测试,我发现使用则getIdentifier将无法正常工作(它总是返回0,除非它运行的应用程序)。现在,我手动解析AttributeSet中获得我所需要的所有属性(手柄,内容,方向等)的ID。当GLE处理此,它返回+ ID /柄,而不是资源INT重新presenting它

After testing, I've discovered that using getIdentifier won't work (it always returns 0, unless it's running the app ). Right now, I'm manually parsing the AttributeSet to get the ids of all the attributes I need (handle, content, direction, etc). When the GLE processes this, it returns +id/handle, rather than the resource int representing it.

看来只有将用于GLE的工作就是直接给它的静态R.id INT。与ADT(我在R17),你不能引用图书馆资源的新版本来看​​,我没有看到一个方法来动态喂我需要的资源ID。

It seems the only thing that will work for the GLE is to directly feed it the R.id static int. Seeing with the newer versions of ADT (I'm on r17) you can't reference the library's resources, I don't see a way to dynamically feed the resource ids I need.

现在,似乎得到这个工作的唯一方法是将抽屉本身在用户的项目,我真的想避免,如果在所有可能的源文件。这将意味着在创建一个新项目的任何将其放置在项目的源目录,或添加右键点击子菜单命令来放置它。双方只是看起来像坏的解决方法。

Right now, it seems the only way to get this to work is to place the source file of the drawer itself in the user's project, which I would really like to avoid if at all possible. It would mean either placing it in the project's source dir upon creation of a new project, or adding a right click submenu command to place it. Both just seem like bad workarounds.

推荐答案

是不是真的有什么特别之处设置样式数组。所有 obtainStyledAttributes()做的是让你获得一个批属性的一个呼叫,而无需执行每个属性,它是慢了单独调用。所以设置样式阵列仅仅是属性资源标识符的数组。有的,关于如何在结构特殊要求 - 资源标识符需要在排序顺序,因为这是最优化的一部分,以快速检索它们。所以,如果你想建立自己的数组,你需要确保你把标识有适当排序。如果你不知道识别号码,您可以用code,你表现出能为每一个值...虽然注意,每次通话时间全部安装的做 obtainStyledAttributes( )可能超过删除的性能增益,你会从功能上得到的。

There isn't really anything special about the styleable array. All obtainStyledAttributes() does is allow you to obtain a batch of attributes in one call, instead of having to perform a separate call for each attribute, which is slower. So the styleable array is just an array of resource identifiers for attributes. There are special requirements on how it is structured -- the resource identifiers need to be in sorted order, as this is part of the optimization to quickly retrieving them. So if you want to build your own array, you need to make sure that the identifiers you put there are appropriately sorted. If you don't know the identifier numbers, you can use the code you showed to get the value for each one... though note that doing all of that setup each time you call obtainStyledAttributes() probably more than removes the performance gain you would get from the function.

这篇关于获取动态风格属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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