android - kivy:水平ListView

标签 android python layout kivy

是否可以在 Kivy 框架中创建横向 ListView?

所有示例都展示了如何创建垂直方向的元素列表,但我需要使用水平滚动条水平排列它们。

默认值:


:                   :
|                   |
+-------------------+  ^
|                   |  ^
|     Element 5     |  ^
|                   |  |
+-------------------+  |
|                   |  |
|     Element 6     |  |
|                   |  |
+-------------------+  |
|                   |  |
|     Element 7     |  |
|                   |  v
+-------------------+  v
|                   |  v
:                   :

我愿意:

... --+-----------+-----------+-----------+---- ...
      |           |           |           |
      | Element 5 | Element 6 | Element 7 |
      |           |           |           |
... --+-----------+-----------+-----------+---- ...

   <<<---------------------------------------->>>

最佳答案

方向是硬编码的。如果你看https://github.com/kivy/kivy/blob/master/kivy/uix/listview.py你会发现:

Builder.load_string('''
<ListView>:
    container: container
    ScrollView:
        pos: root.pos
        on_scroll_y: root._scroll(args[1])
        do_scroll_x: False
        GridLayout:
            cols: 1
            id: container
            size_hint_y: None
''')

我将此文件复制到项目目录中并将其替换为:

Builder.load_string('''
<ListView>:
    container: container
    ScrollView:
        pos: root.pos
        on_scroll_y: root._scroll(args[1])
        do_scroll_x: False
        GridLayout:
            rows: 1
            id: container
            size_hint_y: None
''')

并使用此测试代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from listview import ListView
from kivy.uix.gridlayout import GridLayout


class MainView(GridLayout):
    def __init__(self, **kwargs):
        kwargs['cols'] = 2
        super(MainView, self).__init__(**kwargs)

        list_view = ListView(item_strings=[str(index) for index in range(100)])

        self.add_widget(list_view)


if __name__ == '__main__':
    from kivy.base import runTouchApp
    runTouchApp(MainView(width=800))

我设法获得水平列表。但是,它打破了滚动。看起来您必须更多地更改此配置并修改滚动方法才能获得所需的结果。

关于android - kivy:水平ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18215388/

相关文章:

python - 如何在 djangorestframework 中使用 ManytoManyField

python - 选择 3d ndarray 中的第一列

html - 在 2 个不同的列中水平对齐 div

android - 是否有适用于 Mac 或 Windows 的 Miracast 客户端/服务器?

android - 如何从 SQLite 数据库中存储和获取大文本数据?

android - 如何在 TopAppBar 上的 IconButton 下方添加文本?

android - 使用存储在 android sqlite 数据库中的数据作为 weka 的输入

Python:只返回整数

java - 为什么我应该使用 formlayout/formdata 而不是 setBounds?

android - View 的 setX()、setY() 和 layout()