2010-07-29 124 views
0

我想引用helloWorld XML佈局中的自定義視圖,但是我得到以下異常: 錯誤地誇大了類acme.my.MyTextView。無法在佈局XML中引用自定義視圖

但是,我能夠實例化視圖並將其手動添加到主內容視圖。自定義視圖是從它自己的XML佈局構建的。我如何得到這個工作?

public class MyTextView extends LinearLayout { 

MyTextView(Context context){ 
    super(context); 

    LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    layoutInflater.inflate(R.layout.my_text_view,this); 
} 

MyTextView(Context context, AttributeSet attrs){ 
    super(context, attrs); 

    LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    layoutInflater.inflate(R.layout.my_text_view,this); 
} 
} 



<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" 
    /> 

    <view class = "acme.my.MyTextView" 
android:id="@+id/myView" 
android:orientation="vertical" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"   
/> 
+0

@fredrick:通常情況下,有一個「所造成的」下面你舉一個解釋一個例外有關錯誤的更多內容 – CommonsWare 2010-07-29 04:37:28

+0

MyTextView(Context,AttributeSet) 雖然我在構造函數的頂部有一個斷點,但它永遠不會被擊中。 – Fredrick 2010-07-29 12:19:13

+0

嗯,我用非常簡單的佈局完成了它,它似乎工作。弄清楚在更復雜的佈局中打破了什麼。 – Fredrick 2010-07-29 22:33:18

回答

0

你要這樣稱呼它

<com.example.MyLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 

也會使你的構造函數公共