我的代码在将小部件放入父视图之前动态调整小部件的大小。宽度简单地计算为每个窗口小部件的整数并独立分配。问题在于,当一个较大的小部件出现时,所有之前的小部件大小完全相同,即使它们已经放置并且未受影响。
我打开调试器进行调查。原来每个小部件的宽度放在内存中的一个位置。因此它们几乎都是相同的,并且为一个小部件更改它会导致其他人适当地改变。这是一个代表问题的脏应用程序:
import UIKit
import Contacts
import Firebase
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
//contacts
var contactStore = CNContactStore()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
FIRApp.configure() <-- Use of unresolved identifier 'FIRApp'
// Override point for customization after application launch.
return true
}
我怀疑问题可能出现在Message小部件的kv定义中,尤其是此行from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.uix.gridlayout import GridLayout
Builder.load_string('''
<ScrollView>:
canvas:
Color:
rgba: 1, 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
<Message>:
x: 5
width: 500
canvas:
Color:
rgba: 1,0,0,1
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
pos: root.pos
height: self.height
canvas:
Color:
rgba: 1,1,1,1
RoundedRectangle:
pos: root.x, root.y
size: self.width, self.height
TextInput:
pos: root.pos
size: root.size
id: msg
background_color: 0, 0, 0, 0
''')
class Chat(Screen):
pass
class Message(Widget):
pass
class ChatApp(App):
def width_modify(self):
min_width = 160
max_width = 320
max_line = 0
for i in self.msg_stack[-1].ids['msg']._lines_labels:
if i.width > max_line:
max_line = i.width
curr_width = max_line + 15
return curr_width
def msg_in(self, btn):
msg_stack = self.msg_stack
msg = self.tx1_main.text.strip("\n ")
msg_stack.append(Message())
msg_stack[-1].ids['msg'].text = msg
msg_stack[-1].size_hint = [None, None]
msg_stack[-1].width = self.width_modify()
msg_stack[-1].height = (len(msg_stack[-1].ids['msg']._lines_labels) + 1) * (msg_stack[-1].ids['msg'].line_height)
for i in msg_stack[-1].walk():
i.height = msg_stack[-1].height
i.width = msg_stack[-1].width
msg_stack[-1].y = self.sv1_main.height - 5 - self.pr_msg_y - msg_stack[-1].height
self.msg_layout.add_widget(msg_stack[-1])
self.pr_msg_y += msg_stack[-1].height + 5
def build(self):
self.msg_stack = []
self.pr_msg_y = 5
self.chat = Chat()
self.tx1_main = TextInput(size_hint = (0.9, 0.155))
self.sv1_main = ScrollView(pos_hint = {"top":0.87, "center_x":0.5},
size_hint = (0.97, 0.65))
self.msg_layout = GridLayout(height = 10, cols = 1, size_hint_y = None)
self.msg_layout.bind(minimum_height = self.msg_layout.setter('height'))
self.bt1_main = Button(size_hint = (0.061, 0.078),
pos_hint = {"top":0.097, "center_x":0.951},
on_press = self.msg_in)
self.chat.add_widget(self.tx1_main)
self.chat.add_widget(self.sv1_main)
self.sv1_main.add_widget(self.msg_layout)
self.chat.add_widget(self.bt1_main)
return self.chat
ChatApp().run()
。这是我用于测试目的的一些属性,现在删除它导致我的尺寸打破。而且,评论它似乎并没有解决任何问题。那我该怎么办?
这是一个link来证明这个问题。红色角落不应延伸
答案 0 :(得分:0)
升级到Kivy的最新开发版本(v1.9.2-dev0
)似乎可以解决问题