代码之家  ›  专栏  ›  技术社区  ›  AGrunewald

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

  •  18
  • AGrunewald  · 技术社区  · 16 年前

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

    如果清楚地记录了替代方案是什么,那么这就不是一个真正的问题。例如,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一起工作?

    4 回复  |  直到 8 年前
        1
  •  16
  •   Melebius    8 年前

    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

    你可以用 < 定义要作为组检索的任何属性列表,包括您自己的属性和平台定义的属性。

    就文档中的这些内容而言,在可样式化数组周围有很多java文档,这使得它们在文档中很有用,所以它们就留在了文档中。但是,随着数组的更改,例如添加新属性,常量的值可能会更改,因此平台常量不能在SDK中(请不要使用任何技巧尝试访问它们)。无论如何,应该不需要使用平台版本,因为它们每个都只是为了实现框架的一部分,并且创建自己的版本非常简单,如图所示。

        2
  •  15
  •   Daniel Bob T    7 年前

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

    将ActainStyledAttributes更改为c。ActainStyledAttributes应该可以工作

        3
  •  7
  •   LanDenLabs    12 年前

    在具有自己默认样式的自定义视图中拉出标准属性(背景)的示例。在本例中,自定义视图 延伸 . 我为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();
                }
            }
        }
    

    以下是my 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>
    
        4
  •  5
  •   CommonsWare    16 年前

    issue 您可能希望在其上启动,以便接收更新。

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