2013-03-11 54 views
2

我想玩android,所以我採取了一些我現有的和修改它。這是一個主 - 細節應用程序,在Eclipse中使用默認設置生成,並且沒有新文件。我把位於COM ..的ItemDetailFragment.java的代碼,並修改它,所以它會得出一些測試在屏幕上(0,0)如何在主細部應用程序中使用畫布繪製文字?

下面是我修改

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_item_detail, 
       container, false); 
     //customImageView custom = (customImageView) inflater.inflate(R.layout.fragment_item_detail, container, false); 

     // Show the dummy content as text in a TextView. 
     if (mItem != null) { 
      //((ImageView) rootView.findViewById(R.id.item_detail_)).setImageResource(R.drawable.ic_launcher); 
      ((customImageView) rootView.findViewById(R.id.item_detail__)).invalidate(); 
     } 
     return rootView; 
    } 
    private class customImageView extends View { 
    /*To clarify I added this class myself b/c the android developer guide on Canvas and Drawables says to*/ 

     public customImageView(Context context, AttributeSet attrs, int defStyle) { 
      super(context, attrs, defStyle); 
      // TODO Auto-generated constructor stub 
     } 
     @Override 
     public void onDraw(Canvas canvas) { 
      canvas.drawText("Some Example Text", 0, 0, new Paint()); 
     } 
    } 
} 

哦代碼,這是我的/res/layout/fragment_item_detail.xml與申報

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


    <TextView 
     android:id="@+id/item_detail" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:padding="16dp" 
     android:textIsSelectable="true" /> 
<!-- Autogenerated --> 

<ImageView 
     android:id="@+id/item_detail_" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
     <!-- android:src="@drawable/android" /> --> 
    <!-- I added this in --> 

<customImageView 
     android:id="@+id/item_detail__" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
    <!-- This is the relevant part, I added this in --> 

</LinearLayout> 

然而,當應用程序是在模擬器上運行,我得到this big list of errors

能someo ne請幫我理解爲什麼會發生這種情況,以及如何解決這個問題?

回答

1

這可能是因爲您的視圖類是私人的,請嘗試將其設置爲公開。另外,在XML上添加自定義視圖時,需要包含它的完整位置,因此如果它位於package com.pkg上的名爲clazz的類中,則需要編寫

<com.pkg.clazz.customImageView 
     ... /> 
相關問題