Kivy:调暗Android导航按钮

时间:2015-01-08 19:33:16

标签: android python kivy

我想在Kivy应用程序中调暗导航按钮。如何在Java中执行此操作:https://developer.android.com/training/system-ui/dim.html 但我无法弄清楚如何使用pyjnuis或其他东西在Kivy中做到这一点。 有人以前做过这个吗?

由于

1 个答案:

答案 0 :(得分:1)

是的,你可以通过pyjnius来做到这一点。唯一需要注意的是,您无法从UI线程外部修改Android UI组件。

首先,您将使用pyjnius导入Java类:

from jnius import autoclass
PythonActivity = autoclass('org.renpy.android.PythonActivity')
View = autoclass('android.view.View')

然后可以修改SystemUI(仅限UI线程):

PythonActivity.mActivity.getWindow().getDecorView()
    .setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE)

以下是一个完整的例子:

import kivy
kivy.require('1.8.0')

from kivy.app import App
from kivy.lang import Builder

from jnius import autoclass
PythonActivity = autoclass('org.renpy.android.PythonActivity')
View = autoclass('android.view.View')

from android.runnable import run_on_ui_thread

root = Builder.load_string('''
BoxLayout:
    orientation: 'vertical'
    Button:
        text: 'Dim'
        on_press: app.dim()
    Button:
        text: 'Undim'
        on_press: app.undim()
''')

class DimTestApp(App):
    def build(self):
        return root

    @run_on_ui_thread
    def set_systemui_visibility(self, options):
        PythonActivity.mActivity.getWindow().getDecorView().setSystemUiVisibility(options)

    def dim(self, *args):
        self.set_systemui_visibility(View.SYSTEM_UI_FLAG_LOW_PROFILE)

    def undim(self, *args):
        self.set_systemui_visibility(0)

if __name__ == '__main__':
    DimTestApp().run()

You can download a test APK if you'd like(链接有效期至2015-04-09)。