试图获得code的属性值返回不正确的值 [英] Attempting to get attribute values in code returns incorrect values

查看:142
本文介绍了试图获得code的属性值返回不正确的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提取一个样式资源(仅在属性兴趣落在了TextAppearance集团)多重属性。

I wish to extract multiple attributes from a style resource (only interested in attributes that fall in the TextAppearance Group)

定义为这样风格

<style name="Label" parent="@android:style/TextAppearance.Small">
    <item name="android:textColor">@color/floatlabel_text</item>
    <item name="android:textSize">8dp</item>
    <item name="android:textStyle">bold</item>
</style>

第一次尝试

首先,我试过的TextView(线663-731),它如何实现的,但后来我发现我们没有获得的 com.android.internal.R

这就是为什么我切换到该解决方案: http://stackoverflow.com/a/7913610/3922891

Which is why I switched to this solution: http://stackoverflow.com/a/7913610/3922891

因此​​,我创建textAppearanceAttr更换com.android.internal.R.styleable.TextAppearance(只包含10/13 TextAppearance属性我感兴趣的)

So I created textAppearanceAttr to replace com.android.internal.R.styleable.TextAppearance (only contains 10/13 TextAppearance attributes I am interested in)

int[] textAppearanceAttr = new int[]{    
        android.R.attr.textColor,
        android.R.attr.textSize,
        android.R.attr.typeface,
        android.R.attr.fontFamily,
        android.R.attr.textStyle,
        android.R.attr.textAllCaps,
        android.R.attr.shadowColor,
        android.R.attr.shadowDx,
        android.R.attr.shadowDy,
        android.R.attr.shadowRadius};

下面是我如何使用它。我得到的样式的资源ID(资源被clTextAppearance属性引用)

Here is how I used it. I get the style's resource id (resource is referenced by a clTextAppearance attribute)

   int ap = a.getResourceId(R.styleable.CustomLabelLayout_clTextAppearance, android.R.style.TextAppearance_Small);
   TypedArray appearance = mContext.obtainStyledAttributes(ap, textAppearanceAttr);

这是我得到的属性(在上面的链接的答案依然遵循):

And here is how I get the attributes (still following the answer at the above link):

    mLabelTextColor = appearance.getColorStateList(0);
    mLabelTextSize = appearance.getDimensionPixelSize(1, 15);
    mLabelTypeface = appearance.getInt(2, -1);
    mLabelFontFamily = appearance.getString(3);
    mLabelTextStyle = appearance.getInt(4, -1);
    (5 more...)

本期

似乎只有在第一属性获取设置,每隔要么设置与默认值或空。

Current Issue

It seems that only the first attribute gets set, every other either sets with the default or null.

个别阵列:

int[] textSizeAttr = new int[] { android.R.attr.textSize};
int[] textStyleAttr = new int[] { android.R.attr.textStyle};

和得到像这样的属性。

    appearance.recycle();
    appearance = mContext.obtainStyledAttributes(ap, textSizeAttr);
    mLabelTextSize = appearance.getDimensionPixelSize(0, 15);
    appearance.recycle();
    appearance = mContext.obtainStyledAttributes(ap, textStyleAttr);
    mLabelTextStyle = appearance.getInt(0, -1);
    appearance.recycle();

现在这样做是一种浪费。

Now doing this is such a waste.


  1. 我想知道为什么让所有的属性立刻不起作用。

  2. 是否有解决方案(所有额外的工作是没有必要的)?

修改1

我发现类似的东西在这里: http://stackoverflow.com/a/13952929/3922891
出于某种原因,它的工作原理。直到我添加更多属性到数组,然后一切都变得乱哄哄。

I found something similar here: http://stackoverflow.com/a/13952929/3922891 And for some reason it works. Until I add more attributes to the array then everything becomes kerfuffle.

例如:

 int[] attrs = {android.R.attr.textColor,
            android.R.attr.textSize,
            android.R.attr.background,
            android.R.attr.textStyle,
            android.R.attr.textAppearance,
            android.R.attr.textColorLink,
            android.R.attr.orientation,
            android.R.attr.text};

如果我使用上述阵列它的作品获得文字。

If I get text using the above array it works.

String text = ta.getString(7);

但是,如果我阵列更改为它下面的失败(与android.R.attr.shadowColor取代android.R.attr.orientation)

But if I change the array to the below it fails (replaced android.R.attr.orientation with android.R.attr.shadowColor)

int[] attrs = {android.R.attr.textColor,
            android.R.attr.textSize,
            android.R.attr.background,
            android.R.attr.textStyle,
            android.R.attr.textAppearance,
            android.R.attr.textColorLink,
            android.R.attr.shadowColor,
            android.R.attr.text};

这是怎么回事? (问题1)

Why is this happening? (Question #1)

推荐答案

我觉得我有一个想法,为什么它的发生。看起来,如果ID是没有排序,你的问题。 文字颜色例如具有最低的 INT 价值,这就是为什么它开始工作被放置在数组中的第一个位置。

I think I have an idea why it's happening. Looks like if IDs are not sorted, you get issues. textColor for example has the lowest int value, that's why it starts working being placed on the first position in the array.

如果你看看 R.java 与你的风格化,你会看到Android的资源编译器来分类的ID为您服务。那是,如果你在 attrs.xml 风格化的声明,如果你手动创建ID的阵列可能无法正常工作,为什么它总是有效。

If you take a look at R.java with your stylable, you'll see that android resource compiler has sorted the IDs for you. That's why it always works if you declare stylable in attrs.xml and may not work if you manually created the arrays of IDs.

我相信这是一个性能原因的ID进行排序。如果它们进行排序,然后属性可以从的AttributeSet 使用一个穿越替代N遍历的N ID的情况下被读取。

I believe there is a performance reason for IDs to be sorted. If they are sorted then attributes can be read from the AttributeSet using one traversal instead of N traversals in case of N ids.

更新:
我看了看源$ C ​​$ c和它证明了我的想法。
Context.obtainStyledAttributes()调用JNI方法AssetManager.applyStyle()。
你可以在这里找到源代码:

UPDATE: I took a look at the source code and it proves my idea. Context.obtainStyledAttributes() calls JNI method AssetManager.applyStyle(). You can find source here:

<一个href=\"https://android.googlesource.com/platform/frameworks/base.git/+/android-4.3_r2.1/core/jni/android_util_AssetManager.cpp\" rel=\"nofollow\">https://android.googlesource.com/platform/frameworks/base.git/+/android-4.3_r2.1/core/jni/android_util_AssetManager.cpp

在1001线,你会发现一个while循环,其中九(中提取XML索引属性阵列)始终递增,从不重置为0,这意味着如果文字颜色是在阵列(变量SRC的最后一个索引在code),那么我们将永远不会得到该属性。

On line 1001 you'll find a while loop where ix (index in the extracted XML attributes array) is always incremented and never reset to 0. This means if textColor is the last index in the array (variable "src" in the code) then we'll never get to that attribute.

这篇关于试图获得code的属性值返回不正确的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆