2011-07-04 32 views
0

使用Eclipse,我創建了一個包含自定義組件(MyTextView,繼承自android.widget.TextView)的簡單Android項目。 我創建該文件後,在這裏我添加的屬性到這個類,如下面規定的attrs.xml:Eclipse不顯示我的自定義屬性(attrs.xml)

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="MyTextView" > 
     <attr name="AttributeOne" format="integer"/> 
    </declare-styleable> 
</resources> 

我在main.xml中加入一個目的線性佈局:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:mt="http://schemas.android.com/apk/res/myprogram.custom" 
    android:orientation="vertical" 
    android:layout_height="fill_parent" android:layout_width="fill_parent"> 
    <myprogram.custom.MyTextView 
     android:text="MyTextView" 
     android:id="@+id/myTextView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
    </myprogram.custom.MyTextView> 
</LinearLayout> 

當我打開main.xml,並選擇選項卡'圖形佈局',選擇我的組件時,自定義屬性不會出現在Eclipse的選項卡'屬性'中,而是類繼承的原始屬性() .widget.TextVie w)出現。
這是一個錯誤?提交我的自定義屬性的過程是什麼? 還是我錯了?

謝謝。 盧西亞諾。

+0

if(!isInEditMode()) { // Your custom code that is not letting the Visual Editor draw properly // i.e. thread spawning or other things in the constructor } 

細節你嘗試再次關閉您的項目,並打開它?我注意到,除非我重新加載項目,否則Eclipse中的圖形佈局似乎不會更新(當您添加主題時)的主題。 – Gregory

+1

是的,仍然不起作用... – Luciano

回答

2

我剛剛重新啓動Eclipse,它的工作! :D

+1

耶,什麼是解決方案!這意味着,當你認爲自己做得正確時,它似乎是錯誤的,所以你可能會將你的代碼重新編譯爲無限大,儘管你只需要重新啓動環境 - 這就是Eclipse的全部問題。您可以處理一個不存在的錯誤。 – Deukalion

0

要允許Android Developer Tools與您的視圖進行交互,至少您必須提供一個構造函數,該對象將ContextAttributeSet對象作爲參數。該構造函數允許佈局編輯器創建和編輯視圖的實例。

class PieChart extends View 
{ 
    public PieChart(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 
    } 
} 

,或者如果還沒有那麼解決您可以在自定義視圖做到這一點:的isInEditMode