2012-04-13 49 views
3

圖庫視圖組件的典型應用就像設置ListView + ArrayAdapter工作流程一樣。有沒有辦法通過聲明式定義一個通過XML的圖庫來消除這種開銷?如何通過XML在圖庫中定義子視圖

<Gallery> 
    <TextView text="Screen 1 - Swipe to Next screen"/> 
    <TextView text="Screen 2 - Swipe to Prev screen"/> 
</Gallery> 

如果情況並非如此,那麼需要怎樣的工作?

回答

3

您不能像在代碼中那樣在佈局xml文件中直接定義AdapterView(Galler,Spinner,ListView,...)的子視圖。

但是,有一種方法可以避免適配器。儘管如此,它的可用性有限。您可以使用android:entries屬性。

<Gallery 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:entries="@array/my_items" > 
</Gallery> 

將對數組資源的引用作爲屬性的值。

<string-array name="my_items"> 
     <item>text1</item> 
     <item>text2</item> 
     <item>...</item> 
</string-array> 

該圖庫將填充數組中的文本。 我想你只能使用文本來填充圖庫(或任何其他AdapterView)這種方式,它不適用於更復雜的視圖。您必須使用適配器才能使用更復雜的項目填充圖庫。

+0

男人是如此的真棒。我不知道這個。 – 2012-04-13 19:26:07

相關問題