所以我环顾四周发现 android.R.styleable 不再是 SDK 的一部分,尽管它仍然有文档记录 这里.

如果清楚地记录了替代方案是什么,那实际上就不是问题了。例如 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();

那么如何获得 backgroundDimAmount 没有得到 int[]android.R.styleable.Theme?

我必须坚持什么 obtainStyledAttributes(int []) 为了让它与SDK一起工作?

有帮助吗?

解决方案

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

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

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

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

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

就文档中的这些内容而言,有很多关于可样式数组的 java 文档,这使得它们在文档中非常有用,因此它们被保留在那里。然而,随着数组的变化,例如添加新属性,常量的值可能会发生变化,因此平台常量不能位于 SDK 中(请不要使用任何技巧来尝试访问它们)。无论如何,应该没有必要使用平台框架,因为它们每个都只是用于框架部分的实现,并且创建您自己的平台框架很简单,如下所示。

其他提示

在示例中,他们省略了对上下文“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;
}

将 acquireStyledAttributes 更改为 c.obtainStyledAttributes 应该可以工作

在具有自己的默认样式的自定义视图中提取标准属性(背景)的示例。在此示例中,自定义视图 密码网格 延伸 网格布局. 。我为 PasswordGrid 指定了一个样式,它使用标准 android 属性设置背景图像 安卓:背景.

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();
            }
        }
    }

这是我的 styles.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>

这似乎是 SDK 中的一个错误。我已经提交了一份 问题 您可能希望对其加注星标,以便收到其更新。

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

Class clazz=Class.forName("android.R$styleable");
int i=clazz.getField("Theme_backgroundDimAmount").getInt(clazz);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top