1
我剛剛擴展的LinearLayout和使用簡單的XML佈局一個簡單的自定義視圖:如何在EditMode中擴充佈局?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<ImageView
android:id="@+id/iv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_image1" />
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text2"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
而Java代碼:
public class MyCustomView extends LinearLayout {
public MyCustomView(Context context) {
super(context);
init(context);
}
public MyCustomView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
@SuppressLint("NewApi")
public MyCustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
if (isInEditMode()) {
// How to inflate R.layout.my_custom_view here?
} else {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.my_custom_view, this, true);
}
}
}
我想,我的自定義視圖,在Eclipse佈局編輯器顯示爲默認佈局(R.layout.my_custom_view)。但它不能膨脹,並顯示異常:
android.content.res.Resources$NotFoundException: Could not resolve resource value: 0x7F03000B.
at android.content.res.BridgeResources.throwException(BridgeResources.java:693)
at android.content.res.BridgeResources.getLayout(BridgeResources.java:271)
at android.view.LayoutInflater.inflate(LayoutInflater.java:395)
at ru.bartwell.myapp.MyCustomView.init(MyCustomView.java:29)
at ru.bartwell.myapp.MyCustomView.<init>(MyCustomView.java:18)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0( at sun.reflect.NativeConstructorAccessorImpl.newInstance( at sun.reflect.DelegatingConstructorAccessorImpl.newInstance( at java.lang.reflect.Constructor.newInstance( at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.instantiateClass(ProjectCallback.java:422)
at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.loadView(ProjectCallback.java:179)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:207)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:135)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:755)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:758)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:727)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:373)
有沒有辦法在EditMode中膨脹默認佈局?
當我這樣做,我得到異常資源$ NotFoundException像描述。 – BArtWell
評論第一個構造函數並嘗試。 – R9J
仍然顯示NotFoundException。 – BArtWell