Gallery View 组件的典型应用就像设置 ListView + ArrayAdapter 工作流。有什么方法可以通过 XML 以声明方式定义画廊来避免这种开销吗?
<Gallery>
<TextView text="Screen 1 - Swipe to Next screen"/>
<TextView text="Screen 2 - Swipe to Prev screen"/>
</Gallery>
如果不是这种情况,需要什么才能起作用?
最佳答案
您不能定义 AdapterView 的 subview (Galler、Spinner、ListView,...)直接在布局 xml 文件中,就像您在代码中所做的那样。
但是,有一种方法可以避免适配器。但它的可用性有限。您可以使用 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),它不适用于更复杂的 View 。您必须使用适配器来使用更复杂的项目填充图库。
关于android - 如何通过 XML 在 Gallery 中定义 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10145444/