如何在 Android 的内部主题中使用 gainStyledAttributes(int [])

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2127177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:39:12  来源:igfitidea点击:

How do I use obtainStyledAttributes(int []) with internal Themes of Android

androidandroid-xmlandroid-resourcesandroid-styles

提问by AGrunewald

So I have looked around and found out that android.R.styleableis no longer part of the SDK even though it is still documented here.

所以我环顾四周,发现它android.R.styleable不再是 SDK 的一部分,即使它仍然记录在这里

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

如果它清楚地记录了替代方案是什么,那真的不是问题。例如 AOSP 日历应用程序仍在使用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();

So how would one get the backgroundDimAmountwithout getting the int[]from android.R.styleable.Theme?

那么如何在backgroundDimAmount没有得到int[]from的情况下得到 theandroid.R.styleable.Theme呢?

What do I have to stick into obtainStyledAttributes(int [])in order to make it work with the SDK?

我必须坚持什么obtainStyledAttributes(int [])才能使其与 SDK 配合使用?

采纳答案by hackbod

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

CustomView API 演示展示了如何检索样式属性。视图的代码在这里:

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

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 <declare-styleable>section here:

用于检索文本、颜色和大小的可样式化数组在<declare-styleable>此处的部分中定义:

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

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

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

您可以使用<declare-styleable>定义要作为一个组检索的任何属性列表,其中包含您自己的和平台定义的属性。

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.

就文档中的这些内容而言,围绕可样式化数组有很多 java 文档,这使得它们在文档中很有用,因此它们被保留在那里。但是随着数组的变化,比如添加了新的属性,常量的值会发生变化,所以平台的不能在SDK中(请不要使用任何技巧来尝试访问它们)。无论如何都不需要使用平台的,因为它们每个都只是为了实现框架的一部分,创建自己的框架是微不足道的,如这里所示。

回答by kpasgma

In the example, they left out the reference to the Context 'c':

在示例中,他们省略了对 Context 'c' 的引用:

public ImageAdapter(Context c) {
    TypedArray a = c.obtainStyledAttributes(R.styleable.GalleryPrototype);
    mGalleryItemBackground = a.getResourceId(
            R.styleable.GalleryPrototype_android_galleryItemBackground, 0);
    a.recycle();
    return mGalleryItemBackground;
}

Changing obtainStyledAttributes to c.obtainStyledAttributes should work

将获取样式属性更改为 c.obtainStyledAttributes 应该可以工作

回答by LanDenLabs

Example of pulling out standard attribute (background) in a custom view which has its own default style. In this example the custom view PasswordGridextends GridLayout. I specified a style for PasswordGrid which sets a background image using the standard android attribute android:background.

在具有自己默认样式的自定义视图中提取标准属性(背景)的示例。在这个例子中,自定义视图PasswordGrid扩展了GridLayout。我为 PasswordGrid 指定了一种样式,它使用标准的 android 属性android:background设置背景图像。

public class PasswordGrid extends GridLayout {

    public PasswordGrid(Context context) {
        super(context);
        init(context, null, 0);
    }

    public PasswordGrid(Context context, AttributeSet attrs) {
        super(context, attrs, R.attr.passwordGridStyle);
        init(context, attrs, 0);
    }

    public PasswordGrid(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs, defStyle);
    }

    private void init(Context context, AttributeSet attrs, int defStyle) {
        if (!isInEditMode()) {

            TypedArray stdAttrs = context.obtainStyledAttributes(attrs,
                    new int[] { android.R.attr.background },  // attribute[s] to access
                    defStyle, 
                    R.style.PasswordGridStyle);  // Style to access

           // or use any style available in the android.R.style file, such as
           //       android.R.style.Theme_Holo_Light

            if (stdAttrs != null) {
                Drawable bgDrawable = stdAttrs.getDrawable(0);
                if (bgDrawable != null)
                    this.setBackground(bgDrawable);
                stdAttrs.recycle();
            }
        }
    }

Here is part of my styles.xml file:

这是我的 style.xml 文件的一部分:

 <declare-styleable name="passwordGrid">
    <attr name="drawOn" format="color|reference" />
    <attr name="drawOff" format="color|reference" />
    <attr name="pathWidth" format="integer" />
    <attr name="pathAlpha" format="integer" />
    <attr name="pathColor" format="color" />
 </declare-styleable>



  <style name="PasswordGridStyle" parent="@android:style/Widget.GridView" >  
      <!--  Style custom attributes.  -->
      <item name="drawOff">@drawable/ic_more</item>
      <item name="drawOn">@drawable/ic_menu_cut</item>
      <item name="pathWidth">31</item>
      <item name="pathAlpha">129</item>
      <item name="pathColor">@color/green</item>

      <!-- Style standard attributes -->
      <item name="android:background">@drawable/pattern_bg</item>
</style>

回答by CommonsWare

This appears to be a bug in the SDK. I have filed an issueon it, which you may wish to star so as to receive updates on it.

这似乎是 SDK 中的一个错误。我已经提交了一个问题,您可能希望为它加注星标,以便接收有关它的更新。

As a worksaround, you can use reflection to access the field:

作为一种解决方法,您可以使用反射来访问该字段:

Class clazz=Class.forName("android.R$styleable");
int i=clazz.getField("Theme_backgroundDimAmount").getInt(clazz);