android– 尝试在代码中获取属性值会返回不正确的值

android– 尝试在代码中获取属性值会返回不正确的值,第1张

概述我希望从样式资源中提取多个属性(仅对属于TextAppearance组的属性感兴趣)风格定义如此<stylename="Label"parent="@android:style/TextAppearance.Small"><itemname="android:textColor">@color/floatlabel_text</item><itemname="a

我希望从样式资源中提取多个属性(仅对属于TextAppearance组的属性感兴趣)

风格定义如此

<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

部分解决方案

这就是我切换到这个解决方案的原因:https://stackoverflow.com/a/7913610/3922891

所以我创建了textAppearanceAttr来替换com.androID.internal.R.styleable.TextAppearance(只包含我感兴趣的10/13 TextAppearance属性)

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属性引用)

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

以下是我获取属性的方法(仍然按照上面的链接回答):

    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...)

目前的问题

似乎只有第一个属性被设置,其他每个属性都设置为默认值或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();

现在这样做是浪费.

问题

>我想知道为什么一次获取所有属性不起作用.
>是否有解决方案(不需要所有额外工作)?

编辑1

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

例:

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

如果我使用上面的数组得到文本就可以了.

String text = ta.getString(7);

但是,如果我将数组更改为以下它失败(用androID.R.attr.shadowcolor替换androID.R.attr.orIEntation)

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)

解决方法:

我想我知道为什么会这样.看起来如果ID没有排序,你会遇到问题.例如textcolor具有最低的int值,这就是它开始工作放置在数组中的第一个位置的原因.

如果你看看你的styable的R.java,你会发现androID资源编译器已经为你排序了ID.这就是为什么它总是有效,如果你在attrs.xml中声明styable,如果你手动创建ID数组可能无法工作.

我相信有一个性能原因可以对ID进行排序.如果对它们进行了排序,那么在N IDs的情况下,可以使用一次遍历而不是N次遍历从AttributeSet中读取属性.

更新:
我看了一下源代码,它证明了我的想法.
Context.obtainStyledAttributes()调用JNI方法AssetManager.applyStyle().
你可以在这里找到来源:

https://android.googlesource.com/platform/frameworks/base.git/+/android-4.3_r2.1/core/jni/android_util_AssetManager.cpp

在第1001行,你会发现一个while循环,其中ix(提取的XML属性数组中的索引)总是递增并且永远不会重置为0.这意味着textcolor是数组中的最后一个索引(代码中的变量“src”)那么我们永远不会达到那个属性.

总结

以上是内存溢出为你收集整理的android – 尝试在代码中获取属性值会返回不正确的值全部内容,希望文章能够帮你解决android – 尝试在代码中获取属性值会返回不正确的值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1097582.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存