有一个 style文件
文件中的部分代码如下<style name="style_text_title">
<item name="android:textSize">25px</item>
<item name="android:textColor">#ec9237</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
</style>TypedArray arr = context.getResources().obtainTypedArray(resId);
然后开始遍历
for(int i=0; i<arr.length(); i++){
key = arr.getString(i);//??? 不知道如何获取key 就是item的name属性
value = arr.getText(i).toString();
Log.i(TAG, key + "---" + value);
Log.i(TAG, arr.toString());
}
我捣鼓了好久。也没办法实现, 求助有设么方法可以获取指定资源ID的xml定义(注意最好用系统内置的方法, 原因是效率高)比如说
obj.getBySomeMethod(R.id.style_text_title) // 我可以获取到整个 xml的定义(如顶部xml)

解决方案 »

  1.   

    应该是这个方法吧
    obtainStyledAttributes();看官方TextView源代码 很长不全贴了
     TypedArray a =
                context.obtainStyledAttributes(
                    attrs, com.android.internal.R.styleable.TextView, defStyle, 0);....
    ...
    ..
    .
     /*
             * Look the appearance up without checking first if it exists because
             * almost every TextView has one and it greatly simplifies the logic
             * to be able to parse the appearance first and then let specific tags
             * for this View override it.
             */
            TypedArray appearance = null;
            int ap = a.getResourceId(com.android.internal.R.styleable.TextView_textAppearance, -1);
            if (ap != -1) {
                appearance = context.obtainStyledAttributes(ap,
                                    com.android.internal.R.styleable.
                                    TextAppearance);
            }
            if (appearance != null) {
                int n = appearance.getIndexCount();
                for (int i = 0; i < n; i++) {
                    int attr = appearance.getIndex(i);                switch (attr) {
                    case com.android.internal.R.styleable.TextAppearance_textColorHighlight:
                        textColorHighlight = appearance.getColor(attr, textColorHighlight);
                        break;                case com.android.internal.R.styleable.TextAppearance_textColor:
                        textColor = appearance.getColorStateList(attr);
                        break;                case com.android.internal.R.styleable.TextAppearance_textColorHint:
                        textColorHint = appearance.getColorStateList(attr);
                        break;                case com.android.internal.R.styleable.TextAppearance_textColorLink:
                        textColorLink = appearance.getColorStateList(attr);
                        break;                case com.android.internal.R.styleable.TextAppearance_textSize:
                        textSize = appearance.getDimensionPixelSize(attr, textSize);
                        break;                case com.android.internal.R.styleable.TextAppearance_typeface:
                        typefaceIndex = appearance.getInt(attr, -1);
                        break;                case com.android.internal.R.styleable.TextAppearance_textStyle:
                        styleIndex = appearance.getInt(attr, -1);
                        break;
                    }
                }            appearance.recycle();
            }
    ....
    ..
    .
      

  2.   

    obtainStyledAttributes  这样子获取的还 是一个   TypedArray 都一样啊
      

  3.   

    这个问题我以前回复过:看看下面这个链接就知道了
    http://www.cmd100.com/bbs/forum.php?mod=viewthread&tid=178996&page=1#pid194335
      

  4.   

    继续xml...., 不想用那种方法, 太不自由了