我覺得Luksprog這是錯的,我有一個簡單的解決方案來訪問您的自定義組件 「SRC」 數據,而無需設置樣式,只是在調用的AttributeSet:
attrs.getAttributeResourceValue( 「http://schemas.android.com/apk/res/android」, 「SRC」 ,0);
在這裏你可以看到我的例子,使位圖大小更便宜jeje。
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
int src_resource = attrs.getAttributeResourceValue("http://schemas.android.com/apk/res/android", "src", 0);
this.setImageBitmap(getDrawable(getResources(),src_resource));
}
public static Bitmap getDrawable(Resources res, int id){
return BitmapFactory.decodeStream(res.openRawResource(id));
}
現在,您將在XML是這樣的:
<com.example.com.jfcogato.mycomponent.CustomView
android:id="@+id/tAImageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/big_image_example"/>
你想在自定義視圖的構造函數中獲取可繪製的'my_test_image'嗎? – Luksprog 2012-04-12 18:45:27
是的。我想通過閱讀佈局xml文件中的'android:src'來創建一個Drawable對象。 – michael 2012-04-12 19:03:07