如何将getsStyledAttributes(int [])与Android的内部主题一起使用

AGr*_*ald 19 android android-xml android-resources android-styles

所以我环顾四周,发现它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();
Run Code Online (Sandbox Code Playgroud)

那么如果backgroundDimAmount没有得到它int[],怎么会得到android.R.styleable.Theme

obtainStyledAttributes(int [])为了使其与SDK一起使用,我需要注意什么?

kpa*_*gma 15

在示例中,他们省略了对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;
}
Run Code Online (Sandbox Code Playgroud)

将obtainStyledAttributes更改为c.obtainStyledAttributes应该可行


hac*_*bod 13

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中(请不要使用任何技巧来尝试访问它们).无论如何都不应该使用平台,因为它们每个都只是用于实现框架的一部分,如此处所示创建自己的平台是微不足道的.

  • 感谢您的信息,帮助相当多.据我所知,一个创建,Xml Styleable与例如.一个属性是"android:backgroundDimAmount",允许App检索正确的数据.关于SDK中的内容.我知道有很多有价值的文档,但是解释为什么Class在SDK文档中而不是在API参考中的实际SDK中可能会在将来为开发人员清理,因为仍然有教程可以做到这一点错误:http://developer.android.com/intl/zh-CN/resources/tutorials/views/hello-gallery.html (2认同)

Lan*_*abs 7

在具有自己的默认样式的自定义视图中拉出标准属性(背景)的示例.在此示例中,自定义视图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();
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是我的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>
Run Code Online (Sandbox Code Playgroud)


Com*_*are 5

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

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

Class clazz=Class.forName("android.R$styleable");
int i=clazz.getField("Theme_backgroundDimAmount").getInt(clazz);
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,我同意我不应该使用它,但应该有替代方案。Gallery View Tutorial 早就应该更新了,因为它仍然显示该代码。 (2认同)