为了在代码中创建 UIGridLayout,我缺少啥?
Posted
技术标签:
【中文标题】为了在代码中创建 UIGridLayout,我缺少啥?【英文标题】:What am I missing in order to create an UIGridLayout in code?为了在代码中创建 UIGridLayout,我缺少什么? 【发布时间】:2020-05-11 20:32:49 【问题描述】:这是一个关于创建 UIGridLayout 的问题。我知道如何使用 Studio 菜单执行此操作,但我尝试仅使用代码来执行此操作。这是我的代码:
local layout = Instance.new("UIGridLayout")
layout.Name = "UIGridLayout"
layout.Parent = script.Parent
layout.FillDirection = Enum.FillDirection.Horizontal
layout.CellPadding = UDim2.new(0, 100, 0, 5)
layout.CellSize = UDim2.new(0, 200, 0, 200)
layout.HorizontalAlignment = Enum.HorizontalAlignment.Center
layout.SortOrder = Enum.SortOrder.LayoutOrder
layout.StartCorner = Enum.StartCorner.TopLeft
layout.VerticalAlignment = Enum.VerticalAlignment.Bottom
local teamFrame = Instance.new("Frame")
teamFrame.BackgroundTransparency = 0
teamFrame.LayoutOrder = 0
teamFrame.Parent = script.Parent
local lblTeamTag = Instance.new("TextLabel", teamFrame)
lblTeamTag.TextTransparency = 0
lblTeamTag.TextStrokeTransparency = 0
lblTeamTag.Name = "TeamTag"
lblTeamTag.Text = "Team"
lblTeamTag.Size = UDim2.new(0, 200, 0, 50)
lblTeamTag.Position = UDim2.new(0, 0, 0, 0)
local lblPoints = Instance.new("TextLabel", teamFrame)
lblPoints.TextStrokeTransparency = 0
lblPoints.BackgroundTransparency = 0
lblPoints.Name = "Points"
lblPoints.Text = "0"
lblPoints.Size = UDim2.new(0, 200, 0, 150)
lblPoints.Position = UDim2.new(0, 0, 0, 50)
此代码所在的脚本是 ScreenGui 的本地脚本子代。我期待看到这样的东西:
但我一无所获。我知道我很接近。我错过了什么。非常感谢
【问题讨论】:
【参考方案1】:如果您得到的只是 TextLabels 不在正确的位置,那是因为 UIGridLayout 必须在框架内,而不是在框架的同一个父级内。因此,如果您在实例化框架之后实例化 UIGridLayout,然后使框架成为 UIGridLayout 的父级,它应该可以工作。应该这样做,这样您就可以复制和粘贴:
local teamFrame = Instance.new("Frame")
teamFrame.BackgroundTransparency = 0
teamFrame.LayoutOrder = 0
teamFrame.Parent = script.Parent
local layout = Instance.new("UIGridLayout")
layout.Name = "UIGridLayout"
layout.Parent = script.Parent
layout.FillDirection = Enum.FillDirection.Horizontal
layout.CellPadding = UDim2.new(0, 100, 0, 5)
layout.CellSize = UDim2.new(0, 200, 0, 200)
layout.HorizontalAlignment = Enum.HorizontalAlignment.Center
layout.SortOrder = Enum.SortOrder.LayoutOrder
layout.StartCorner = Enum.StartCorner.TopLeft
layout.VerticalAlignment = Enum.VerticalAlignment.Bottom
local lblTeamTag = Instance.new("TextLabel", teamFrame)
lblTeamTag.TextTransparency = 0
lblTeamTag.TextStrokeTransparency = 0
lblTeamTag.Name = "TeamTag"
lblTeamTag.Text = "Team"
lblTeamTag.Size = UDim2.new(0, 200, 0, 50)
lblTeamTag.Position = UDim2.new(0, 0, 0, 0)
local lblPoints = Instance.new("TextLabel", teamFrame)
lblPoints.TextStrokeTransparency = 0
lblPoints.BackgroundTransparency = 0
lblPoints.Name = "Points"
lblPoints.Text = "0"
lblPoints.Size = UDim2.new(0, 200, 0, 150)
lblPoints.Position = UDim2.new(0, 0, 0, 50)
【讨论】:
以上是关于为了在代码中创建 UIGridLayout,我缺少啥?的主要内容,如果未能解决你的问题,请参考以下文章
在 Oracle 中创建表会产生“缺少右括号”错误 [重复]
SQL - 在 PARTITION BY 语句中创建层次结构
由于缺少 x-ms-blob-type,在 ADLS Gen2 中创建路径失败?