如何使用obtainStyledAttributes(INT [])与Android的内部主题如何使用、主题、INT、obtainStyledAttributes

2023-09-04 09:30:09 作者:倾酒向涟漪

所以我环顾四周,发现了 android.R.styleable 是,即使它仍然是这里记录的SDK不再一部分:http://developer.android.com/reference/android/R.styleable.html 这不会真的是一个问题,如果它被明确记载的另一种方法是什么。例如,AOSP日历应用程序仍使用 android.R.styleable

So I have looked around and found out that android.R.styleable is no longer part of the SDK even though it is still documented here: http://developer.android.com/reference/android/R.styleable.html That wouldn't really be an issue if it was clearly documented what the alternative is. For example the AOSP Calendar App is still using the android.R.styleable

// Get the dim amount from the theme   
TypedArray a = obtainStyledAttributes(com.android.internal.R.styleable.Theme);
lp.dimAmount = a.getFloat(android.R.styleable.Theme_backgroundDimAmount, 0.5f);
a.recycle();

那么如何将一个获得 backgroundDimAmount 没有得到的 INT [] android.R.styleable.Theme ?我有什么要坚持到 obtainStyledAttributes(INT []),以使其与SDK工作?

So how would one get the backgroundDimAmount without getting the int[] from android.R.styleable.Theme? What do I have to stick into obtainStyledAttributes(int []) in order to make it work with the SDK?

推荐答案

在CustomView API演示展示了如何检索样式属性。在code的观点是在这里:

The CustomView API demo shows how to retrieve styled attributes. The code for the view is here:

https://github.com/android/platform_development/blob/master/samples/ApiDemos/src/com/example/android/apis/view/LabelView.java

用于检索文本,颜色的设置样式阵列,并且大小在节这里定义:

The styleable array used to retrieve the text, color, and size is defined in the section here:

https://github.com/android/platform_development/blob/master/samples/ApiDemos/res/values/attrs.xml#L24

您可以用它来定义要检索的一组属性的任何名单,包括您自己和那些由平台定义的。

You can use to define any list of attributes that you want to retrieve as a group, containing both your own and ones defined by the platform.

至于这些事情都是在文档中,有很多绕设置样式阵列的java文档,使得具有与文档中他们有用的,所以它们已被留在那里。然而,随着阵列变化,如添加新的属性,常量的值可以改变,所以这个平台的人不能在SDK(请不要使用任何技巧,尝试访问它们)。应该没有必要无论如何使用该平台的,因为他们每个那里只是为框架的执行部件,这是微不足道的创建自己的如下图所示。

As far as these things being in the documentation, there is a lot of java doc around the styleable arrays that makes them useful to have in the documentation, so they have been left there. However as the arrays change, such as new attributes being added, the values of the constants can change, so the platform ones can not be in the SDK (and please do not use any tricks to try to access them). There should be no need to use the platform ones anyway, because they are each there just for the implementation of parts of the framework, and it is trivial to create your own as shown here.