如何获取属性集值为" @ color / desktop_purple"

时间:2014-04-22 16:44:53

标签: android android-custom-view

我有两个apks,例如packageA,packageB; 同时我创建了一个名为HomeSuite的自定义视图 这是我的xml布局:

<LinearLayout
    style="@style/page_linearlayout"
    android:layout_marginTop="@dimen/hometopfragment_layout_margintop"
    android:layout_weight="16" >

    <com.my.example.view.HomeSuite
        android:background="@color/desktop_purple"
        android:layout_marginRight="2dp"
        android:layout_weight="1" />

    <com.my.example.view.HomeSuite
        android:background="@color/desktop_grey"
        android:layout_marginRight="2dp"
        android:layout_weight="1" />

    <com.my.example.view.HomeSuite
        android:background="@color/desktop_light_green"
        android:layout_weight="1" />

</LinearLayout>

这是构造函数:

public HomeSuite(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    int n = attrs.getAttributeCount();
    String name ;
    String value ;
    for(int i=0;i<n;i++)
    {
        name = attrs.getAttributeName(i);
        value = attrs.getAttributeValue(i);
    }
    mContext = (Activity) context;
    mPackageManager = mContext.getPackageManager();

}

当我得到name =“background”,并且值=“@ 2131230733”,我期待得到“@ color / desktop_purple”时,我想使用此值作为资源名称来读取资源来自PackageB。

    int id = packageBContext.getResources().getIdentifier(value, "color", packageB);
    return packageBContext.getResources().getColor(id);

我需要知道值“@ color / desktop_purple”,然后我可以弄清楚它的“颜色”相关或“可绘制”相关,之后我可以决定使用getColor(id)或getDrawable(id)。

任何人都可以提供帮助吗?

0 个答案:

没有答案