2011-05-27 37 views
0

歡迎。我正在研究一個項目,並認爲我會捲起一個複合組件。我將它們放在xml中,並寫了一個擴展了LinearLayout的新類。基本上,我需要一個活動中大約五六個人。我想以編程方式在Activity中創建這些內容,因爲我不知道在運行時需要多少個。此外,我創建了一個XML文件來設置它們,我沒有在下面列出。我發現要執行此操作的每種方法都有問題。在運行時創建的複合Android組件

這裏是我做了這個類裏面的方法段延伸的LinearLayout:

public ExtendedLinearLayout(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    ... 
} 

@Override 
protected void onFinishInflate() { 
    super.onFinishInflate(); 
    ((Activity)getContext()).getLayoutInflater().inflate(R.layout.extended_linear_layout, this); 
    ... 
} 

以及活動中,這些似乎是最接近我要找的兩個解決方案。

不知道從哪裏使用這種方法得到的attrs:

ExtendedLinearLayout customViewClass = new ExtendedLinearLayout(this, attrs); 
layout.addView(customViewClass) 

不知道如何指定充氣應該用這種方法使用ExtendedLinearLayout:

LayoutInflater inflater = LayoutInflater.from(this); 
inflater.inflate(R.layout.extended_linear_layout, parent_layout); 

我會可能會使用ListView來解決 - 但對於五到六個項目來說似乎有點矯枉過正。此外,之前曾與Flex合作過,至少似乎是,就像問題的合理解決方案。

感謝, 蘭德爾

---------------- UPDATE -------------------

我試過了下面討論的方法。一旦我設置了一切,我收到了一個InflationException:二進制XML文件行#8:錯誤膨脹類。我感覺像xml看起來很好。我可能會困惑的一件事是在哪裏打電話給充氣器。現在,我在活動調用onFinishInflate()的自定義類的充氣以及如下:

public class ExtendedLinearLayout extends LinearLayout { 
    ... 
public ExtendedLinearLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     this.context = context; 
    } 

    @Override 
    protected void onFinishInflate() { 
     super.onFinishInflate(); 
     LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     inflater.inflate(R.layout.extended_linear_layout, null); 
     ... 
    } 
} 

而且在活動:

public class TestActivity extends Activity { 
    LinearLayout list; 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     list = (LinearLayout) findViewById(R.id.simple_list); 
     runTestCode(); 
    } 
    private void runTestCode() { 
     LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     ExtendedLinearLayout layoutOne = 
       (ExtendedLinearLayout) inflater.inflate(R.layout.extended_linear_layout, list); 
    } 
} 

感謝,並希望大家」我們有時間去看看。我喜歡通過網上挖掘來弄清楚這些東西,但是自從昨天上午以來,這個東西讓我煮了好幾個小時。我距離將這個重構成ListAdapter大約2英寸。

---------------- UPDATE -------------------

這裏是xml文件

<?xml version="1.0" encoding="utf-8"?> 
<com.anotherandroidblog.ExtendedLinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" 
    > 
    <TextView 
     android:id="@+id/text_one" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="" 
     ></TextView> 
    <TextView 
     android:id="@+id/text_two" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="" 
     ></TextView> 
    <TextView 
     android:id="@+id/text_three" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="2" 
     ></TextView> 
</com.anotherandroidblog.ExtendedLinearLayout> 

回答

0

您需要在XML文件中使用完整的類名:而不是使根元素LinearLayout使其成爲完整的類名ExtendedLinearLayout(包括包名)。然後LayoutInflater將知道該怎麼做。

編輯:此外,attrs選項是不是真的可行(或者至少我不這麼認爲)。我還沒有找到一種方法在運行時生成實際的對象實例:沒有公共的構造函數,我期望這是框架在底層不能訪問的另一個東西。

+1

真棒,感謝忍者風格超級快速響應。我會把它捲入,讓你知道它是如何發生的。另外,是的 - 從我讀過的內容(包括Romain Guy在類似帖子中的回覆),您應該始終使用inflater方法與addView()方法。 – javahead76 2011-05-27 14:51:12

+0

我已經跑了好幾次,現在正在移動的東西,挖掘正確的。我遇到了一種InflaterException,我認爲這與Inflater的時間和方式有關。我在上面的orignal帖子中添加了更新。 – javahead76 2011-05-27 18:32:43

+0

添加布局XML以及我可以看到它的樣子。 – Femi 2011-05-27 19:06:52