2012-04-13 50 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

相关问题