首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

属性定义研究-很浅

2012-09-05 
属性定义研究--很浅在attrs.xml文件中如下定义:?xml version1.0 encodingutf-8?resourcesdecla

属性定义研究--很浅
    在attrs.xml文件中如下定义:

<?xml version="1.0" encoding="utf-8"?><resources><declare-styleable name="MyView"><attr name="textColor" format="color" /><attr name="textSize" format="dimension" /><attr name="imgBackground" format="integer" /><attr name="textPaddingLeft" format="dimension"/><attr name="textPaddingRight" format="dimension"/><!-- Default TestView style. -->  <attr name="testViewStyle" format="reference" /> </declare-styleable></resources>
[color=blue][/color]
   theme.xml 中如下声明:
<resources>      <style name="ddd.OMS" parent="@android:style/Theme">          <item name="testViewStyle">@style/widget.motiontextview</item>    </style>     </resources> 

layout/xxxx.xml下使用,
android:layout_marginRight="15px"android:layout_marginLeft="15px"style="@style/ddd.OMS"android:layout_marginBottom="10px"


这部分代码中要是要使用到自己定义的属性需要如下解析:

TypedArray params = context.obtainStyledAttributes(attrs,                 R.styleable.MyView);TypedArray appearance = null;         int ap = params.getResourceId(R.styleable.MyView_testViewStyle, -1);if (ap != -1) {             appearance = context.obtainStyledAttributes(ap,                                 R.styleable.MyView                                 );         }float textSize = appearance.getDimension(R.styleable.MyView_textSize, 24);不过以上属性需要都是自己定义的,才可以在第二次产生的appearance中获取到

改为如下代码  需要请教:
style="?attr/testViewStyle"暂时不知道如何获取!但是可以通过 int resourceId=attrs.getStyleAttribute()获取testViewStyle 的resource ID,但是现在我还不知道如何获取 testViewStyle 的值!

如果哪位仁兄有办法,请予以回复!谢谢!

热点排行