首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用带有Android内部主题的obtainStyledAttributes(int [])

如何使用带有Android内部主题的obtainStyledAttributes(int [])
EN

Stack Overflow用户
提问于 2010-01-24 21:21:16
回答 4查看 43.1K关注 0票数 21

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

如果清楚地记录了替代方案是什么,那就不是问题了。例如,AOSP Calendar App仍在使用android.R.styleable

代码语言:javascript
复制
// 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();

那么,如果不从android.R.styleable.Theme获取int[],如何获得backgroundDimAmount呢?

我需要在obtainStyledAttributes(int [])中做些什么才能让它与SDK一起工作?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-01-25 14:11:49

CustomView应用编程接口演示展示了如何检索样式化属性。视图的代码如下:

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中(请不要使用任何技巧来访问它们)。无论如何,应该不需要使用平台,因为它们每个都只是为了实现框架的一部分,并且像这里所示的那样创建自己的平台是很容易的。

票数 16
EN

Stack Overflow用户

发布于 2010-09-22 04:49:11

在这个例子中,他们省略了对上下文‘c’的引用:

代码语言:javascript
复制
public ImageAdapter(Context c) {
    TypedArray a = c.obtainStyledAttributes(R.styleable.GalleryPrototype);
    mGalleryItemBackground = a.getResourceId(
            R.styleable.GalleryPrototype_android_galleryItemBackground, 0);
    a.recycle();
    return mGalleryItemBackground;
}

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

票数 15
EN

Stack Overflow用户

发布于 2014-08-17 09:57:19

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

代码语言:javascript
复制
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文件的一部分:

代码语言:javascript
复制
 <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>
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2127177

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档