2011-08-11 44 views
0

我已創建自己的ViewGroup。我已經添加了一個小部件到視圖中,我從Eclipse Design Editor中得到一個錯誤(我需要一個預覽來獲得更快/更好的開發)。自定義ViewGroup

public class MyViewGroup extends LinearLayout { 
    public MyViewGroup(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     this.addView(new EditText(context)); // The Error Line 
    } 
... 
} 

隨着的LayoutParams鴕鳥政策改變什麼:

this.addView(new Button(mcontext), 
      new LinearLayout.LayoutParams(
        LayoutParams.FILL_PARENT, 
        LayoutParams.FILL_PARENT)); 

main.xml中

<?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" 
> 
<...MyViewGroup android:layout_height="match_parent" android:layout_width="match_parent" android:layout_margin="2dip" android:id="@+id/Token01"> </...MyViewGroup> 
</LinearLayout> 

我不看它在模擬器上。如果我刪除「錯誤行」,設計器不會出錯。什麼是我的失敗或無法呈現Eclipse Designer?但是爲什麼我在仿真器/設備上看不到它?我的目標是使用自定義視圖和無XML的ViewGroup創建此部分。

我希望你能幫助我,對不起我的英語不好。

謝謝。

回答

0

如果我理解正確,代碼中沒有錯誤(運行時),但可視化編輯器無法正確顯示它。
我曾經有過類似的問題(不記得),可以通過將可視化編輯器引擎更改爲Honeycomb版本來解決它。
爲了做到這一點,您首先需要安裝一個Honeycomb SDK(API 11或更高版本)。然後,在右上角的可視化編輯器中,您可以選擇它使用的SDK。更改爲3.x.也許這也適用於你。

+0

感謝您的回答。該守則永遠不會運行。可視化編輯器顯示錯誤並且登錄模擬器不顯示EditText。 – Tim

0

我嘗試了以下方法,它的工作原理與Android 2.3.1及更高版本一樣。它不適用於2.2及更早的版本。

public class MyViewGroup extends LinearLayout { 
    public MyViewGroup(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     if (isInEditMode()) { 
     final EditText editText = new EditText(context); 
     editText.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 
       LayoutParams.WRAP_CONTENT)); 
     addView(editText); 
     } 
    } 
} 

另請注意isInEditMode()的用法。

相關問題