Pri*_*ora 14 android android-layout
我希望从样式资源中提取多个属性(仅对属于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)
igr*_*ret 12
我想我知道为什么会这样.看起来如果ID没有排序,你会遇到问题.textColor例如,它具有最低int值,这就是它开始工作放置在数组中的第一个位置的原因.
如果你看看R.java你的stylable,你会发现android资源编译器已经为你排序了ID.这就是为什么它总是有效,如果你声明styable in attrs.xml,如果手动创建ID数组可能不起作用.
我相信有一个性能原因可以对ID进行排序.如果它们被排序,那么可以从AttributeSet使用一个遍历而不是N遍历的N 遍遍读取属性.
更新:我看了一下源代码,它证明了我的想法.Context.obtainStyledAttributes()调用JNI方法AssetManager.applyStyle().你可以在这里找到来源:
在第1001行,你会发现一个while循环,其中ix(提取的XML属性数组中的索引)总是递增并且永远不会重置为0.这意味着textColor是数组中的最后一个索引(代码中的变量"src")那么我们永远不会达到那个属性.
感谢@PrivatMamtora和@igret调查此事!如果问题是必须订购ID,这应该没问题.
private static final int ATTR_PADDING = android.R.attr.padding;
private static final int ATTR_TEXT_COLOR = android.R.attr.textColor;
private static final int ATTR_TEXT_SIZE = android.R.attr.textSize;
private void loadAttributes(Context context, AttributeSet attrs) {
    int[] ids = { ATTR_PADDING, ATTR_TEXT_COLOR, ATTR_TEXT_SIZE};
    Arrays.sort(ids); // just sort the array
    TypedArray a = context.obtainStyledAttributes(attrs, ids);
    try {
        padding = a.getDimensionPixelSize(indexOf(ATTR_PADDING, ids), padding);
        textColor = a.getColor(indexOf(ATTR_TEXT_COLOR, ids), textColor);
        textSize = a.getDimensionPixelSize(indexOf(ATTR_TEXT_SIZE, ids), textSize);
    } finally {
        a.recycle();
    }
}
private int indexOf(int id, int[] ids) {
    for (int i = 0; i < ids.length; i++) {
        if (ids[i] == id) {
            return i;
        }
    }
    throw new RuntimeException("id " + id +  " not in ids"); 
}
让它像这样工作:我定义了一个新的styleable:
<?xml version="1.0" encoding="utf-8"?>
<resources>     
    <declare-styleable name="Label" >
        <attr name="android:textColor" />
        <attr name="android:textSize" />
        <attr name="android:textStyle" />
        <attr name="android:typeface" />
    </declare-styleable>
</resources>
那么这是我的styles.xml:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="Label" parent="@android:style/TextAppearance.Small">
        <item name="android:textColor">#12345678</item>
        <item name="android:textSize">8dp</item>
        <item name="android:textStyle">bold</item>
        <item name="android:typeface">serif</item>
    </style>    
</resources>
最后测试:
public class TextAppearanceTest extends AndroidTestCase {
    public void test() {
        TypedArray a = getContext().obtainStyledAttributes(R.style.Label, R.styleable.Label);
        assertTrue(a.getColor(R.styleable.Label_android_textColor, -1) != -1);
        assertTrue(a.getDimensionPixelSize(R.styleable.Label_android_textSize, -1) != -1);
        assertTrue(a.getInt(R.styleable.Label_android_typeface, -1) != -1);
    }
}
| 归档时间: | 
 | 
| 查看次数: | 5060 次 | 
| 最近记录: |