Kivy:改变ActionOverFlow中ActionButton的背景
Kivy: Change the background of ActionButton in ActionOverFlow
我想更改 ActionBar 中 ActionOverflow 内的 ActionButton 的背景,但是使用按钮的常用方法(background_down、background_normal)似乎不起作用。
[黑色背景的操作按钮] (https://raw.githubusercontent.com/Thomas-Adams/kivy_mananger/master/color_mananger/kivy-actionbutton.png)
奇怪的是,当我单击特定的 ActionButton(例如 'List')时,背景会根据需要更改。然而 'normal' 未按下状态仍然是黑色背景。我也尝试过篡改背景图像 属性,但无济于事。我想念什么?有人对此有线索或提示吗?
#:import get_color_from_hex kivy.utils.get_color_from_hex
<MainView>:
...
ActionButton:
canvas.before:
Color:
rgba: get_color_from_hex('#d500f9')
Rectangle:
size: self.size
pos: self.pos
background_normal: 'electric-violet.png'
background_down: 'electric-violet-lighten.png'
background_disabled: ''
background_disabled_down: ''
background_color: get_color_from_hex('#d500f9')
text: 'Create'
我自己弄明白了,以防其他人被困在这里,我没有使用 ActionButton 小部件,而是编写了我自己的基于 ActionItem 和 Button 的小部件:
class ColorActionButton(ActionItem, Button, EventDispatcher):
color_name = StringProperty()
base_dir = IMAGE_BASE_DIR
b_normal = StringProperty()
b_down = StringProperty()
b_disabled_down = StringProperty()
b_disabled = StringProperty()
def __init__(self, **kwargs):
super().__init__(**kwargs)
def on_color_name(self, instance, value):
self.b_normal = self.base_dir + value + SUFFIX
self.b_down = self.base_dir + value + LIGHTEN_SUFFIX
self.b_disabled_down = self.base_dir + value + DARKEN_SUFFIX
现在使用它就像一个魅力。
另一种可能性是根据 ActionButton
:
创建您自己的 class
class MyActionButton(ActionButton):
real_background_normal = StringProperty('')
并在您的 kv
文件中修改其样式:
<MyActionButton>:
-background_normal: self.real_background_normal
然后在 kv
文件的其他地方,当您使用 MyActionButton
时,只需使用 real_background_normal
而不是 background_normal
:
MyActionButton:
real_background_normal: 'electric-violet.png'
我想更改 ActionBar 中 ActionOverflow 内的 ActionButton 的背景,但是使用按钮的常用方法(background_down、background_normal)似乎不起作用。
[黑色背景的操作按钮] (https://raw.githubusercontent.com/Thomas-Adams/kivy_mananger/master/color_mananger/kivy-actionbutton.png)
奇怪的是,当我单击特定的 ActionButton(例如 'List')时,背景会根据需要更改。然而 'normal' 未按下状态仍然是黑色背景。我也尝试过篡改背景图像 属性,但无济于事。我想念什么?有人对此有线索或提示吗?
#:import get_color_from_hex kivy.utils.get_color_from_hex
<MainView>:
...
ActionButton:
canvas.before:
Color:
rgba: get_color_from_hex('#d500f9')
Rectangle:
size: self.size
pos: self.pos
background_normal: 'electric-violet.png'
background_down: 'electric-violet-lighten.png'
background_disabled: ''
background_disabled_down: ''
background_color: get_color_from_hex('#d500f9')
text: 'Create'
我自己弄明白了,以防其他人被困在这里,我没有使用 ActionButton 小部件,而是编写了我自己的基于 ActionItem 和 Button 的小部件:
class ColorActionButton(ActionItem, Button, EventDispatcher):
color_name = StringProperty()
base_dir = IMAGE_BASE_DIR
b_normal = StringProperty()
b_down = StringProperty()
b_disabled_down = StringProperty()
b_disabled = StringProperty()
def __init__(self, **kwargs):
super().__init__(**kwargs)
def on_color_name(self, instance, value):
self.b_normal = self.base_dir + value + SUFFIX
self.b_down = self.base_dir + value + LIGHTEN_SUFFIX
self.b_disabled_down = self.base_dir + value + DARKEN_SUFFIX
现在使用它就像一个魅力。
另一种可能性是根据 ActionButton
:
class MyActionButton(ActionButton):
real_background_normal = StringProperty('')
并在您的 kv
文件中修改其样式:
<MyActionButton>:
-background_normal: self.real_background_normal
然后在 kv
文件的其他地方,当您使用 MyActionButton
时,只需使用 real_background_normal
而不是 background_normal
:
MyActionButton:
real_background_normal: 'electric-violet.png'