Skip to content

Commit 0a56461

Browse files
authored
Merge pull request #8 from xtuzy/Modify
fix nest constraintlayout size not correct bug
2 parents 780f3b0 + 899e6ef commit 0a56461

File tree

6 files changed

+119
-46
lines changed

6 files changed

+119
-46
lines changed

SharpConstraintLayout.Maui.Example/Pages/MainPage.cs

Lines changed: 86 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -182,33 +182,79 @@ void NestedConstraintLayoutTest(ConstraintLayout page)
182182
{
183183
(Button FirstButton, Button SecondButton, ContentView ThirdCanvas, Label FouthTextBlock, Entry FifthTextBox, Editor SixthRichTextBlock) = CreateControls();
184184

185-
var layout = new ConstraintLayout()
185+
var leftLayout = new ConstraintLayout()
186+
{
187+
ConstrainPaddingTop = 10,
188+
ConstrainPaddingBottom = 10,
189+
ConstrainPaddingLeft = 10,
190+
ConstrainPaddingRight = 10,
191+
DebugName = "left",
192+
Background = new SolidColorBrush(Colors.Red)
193+
};
194+
195+
var rightLayout = new ConstraintLayout()
186196
{
187-
Background = new SolidColorBrush(Colors.Black)
197+
ConstrainPaddingTop = 10,
198+
ConstrainPaddingBottom = 10,
199+
ConstrainPaddingLeft = 10,
200+
ConstrainPaddingRight = 10,
201+
DebugName = "right",
202+
Background = new SolidColorBrush(Colors.Green)
188203
};
189204

190205
using (var pageSet = new FluentConstraintSet())
191206
{
192-
page.AddElement(layout);
207+
page.AddElement(leftLayout, rightLayout);
193208
pageSet.Clone(page);
194-
pageSet.Select(layout)
195-
.CenterTo()
196-
.Width(ConstraintSet.WrapContent)
197-
.Height(ConstraintSet.WrapContent);
209+
pageSet.Select(leftLayout)
210+
.EdgesYTo(null, 10).LeftToLeft(null, 10).RightToLeft(rightLayout, 5)
211+
.Width(SizeBehavier.MatchConstraint)
212+
.Height(SizeBehavier.MatchParent)
213+
.Select(rightLayout).EdgesYTo(null, 10).LeftToRight(leftLayout, 5).RightToRight(null, 10)
214+
.Width(SizeBehavier.MatchConstraint)
215+
.Height(SizeBehavier.MatchConstraint)
216+
;
217+
198218
pageSet.ApplyTo(page);
199-
layout.AddElement(ThirdCanvas);
200-
layout.AddElement(FirstButton);
219+
var leftChildView = new ConstraintLayout()
220+
{
221+
DebugName = "Second",
222+
Background = new SolidColorBrush(Colors.Yellow)
223+
};
224+
leftLayout.AddElement(leftChildView);
225+
using (var layoutSet = new FluentConstraintSet())
226+
{
227+
layoutSet.Clone(leftLayout);
228+
layoutSet
229+
.Select(leftChildView).EdgesTo(null, 20, 20)
230+
.Width(SizeBehavier.MatchConstraint)
231+
.Height(SizeBehavier.MatchConstraint);
232+
layoutSet.ApplyTo(leftLayout);
233+
}
234+
235+
var rightChildView = new ConstraintLayout()
236+
{
237+
DebugName = "Second",
238+
Background = new SolidColorBrush(Colors.Yellow),
239+
ConstrainWidth = ConstraintSet.WrapContent,
240+
};
241+
rightLayout.AddElement(rightChildView);
242+
rightChildView.Add(FirstButton);
201243
using (var layoutSet = new FluentConstraintSet())
202244
{
203-
layoutSet.Clone(layout);
245+
layoutSet.Clone(rightLayout);
204246
layoutSet
205-
.Select(FirstButton).CenterXTo().CenterYTo()
206-
.Width(FluentConstraintSet.SizeBehavier.WrapContent)
207-
.Height(FluentConstraintSet.SizeBehavier.WrapContent)
208-
.Select(ThirdCanvas).EdgesTo(null, 20, 20)
209-
.Width(FluentConstraintSet.SizeBehavier.MatchParent)
210-
.Height(FluentConstraintSet.SizeBehavier.MatchParent);
211-
layoutSet.ApplyTo(layout);
247+
.Select(rightChildView).EdgesTo(null, 20, 20)
248+
.Width(SizeBehavier.WrapContent)
249+
.Height(SizeBehavier.MatchConstraint);
250+
layoutSet.ApplyTo(rightLayout);
251+
252+
using (var set = new FluentConstraintSet())
253+
{
254+
set.Clone(rightChildView);
255+
set.Select(FirstButton).CenterTo();
256+
set.ApplyTo(rightChildView);
257+
}
212258
}
213259
}
214260
}
@@ -535,40 +581,44 @@ private void ConstraintLayoutInListViewTest(ListView listView)
535581
var layout = new ConstraintLayout() { ConstrainWidth = ConstraintSet.MatchParent, ConstrainHeight = ConstraintSet.WrapContent, BackgroundColor = Color.FromRgb(66, 66, 66) };
536582
var title = new Label() { TextColor = Colors.White, FontSize = 30, FontAttributes = FontAttributes.Bold };
537583
title.SetBinding(Label.TextProperty, nameof(Models.MicrosoftNews.Title));
538-
var image = new Image();
539-
image.SetBinding(Image.SourceProperty, nameof(Models.MicrosoftNews.ImageUrl));
584+
//var image = new Image();
585+
//image.SetBinding(Image.SourceProperty, nameof(Models.MicrosoftNews.ImageUrl));
540586
var sourceFrom = new Label() { TextColor = Color.FromRgb(175, 165, 136), FontSize = 12, FontAttributes = FontAttributes.Bold };
541587
sourceFrom.SetBinding(Label.TextProperty, nameof(Models.MicrosoftNews.SourceForm));
542-
var sourceFromeImage = new Image();
543-
sourceFromeImage.SetBinding(Image.SourceProperty, nameof(Models.MicrosoftNews.SourceForm));
544-
layout.AddElement(image, title, sourceFromeImage, sourceFrom);
588+
//var sourceFromeImage = new Image();
589+
//sourceFromeImage.SetBinding(Image.SourceProperty, nameof(Models.MicrosoftNews.SourceForm));
590+
layout.AddElement(
591+
//image,
592+
title,
593+
//sourceFromeImage,
594+
sourceFrom);
545595

546596
var guideLine = new Guideline() { };
547597
layout.AddElement(guideLine);
548598

549599
var littleWindow = new FluentConstraintSet();
550600
littleWindow.Clone(layout);
551601
littleWindow
552-
.Select(guideLine, image, sourceFromeImage, sourceFrom, title).Clear()//需要移除之前的约束
602+
.Select(guideLine,
603+
//image, sourceFromeImage,
604+
sourceFrom, title).Clear()//需要移除之前的约束
553605
.Select(guideLine).GuidelineOrientation(Orientation.X).GuidelinePercent(0.6f)
554-
.Select(image).EdgesXTo().BottomToTop(guideLine)
555-
.Width(SizeBehavier.MatchParent).Height(SizeBehavier.WrapContent)
556-
.Select(sourceFromeImage).LeftToLeft(null, 20).BottomToTop(title, 20)
557-
.Width(SizeBehavier.WrapContent).Height(SizeBehavier.WrapContent)
558-
.Select(sourceFrom).LeftToRight(sourceFromeImage, 20).CenterYTo(sourceFromeImage)
606+
//.Select(image).EdgesXTo().BottomToTop(guideLine).Width(SizeBehavier.MatchParent).Height(SizeBehavier.WrapContent)
607+
//.Select(sourceFromeImage).LeftToLeft(null, 20).BottomToTop(title, 20).Width(SizeBehavier.WrapContent).Height(SizeBehavier.WrapContent)
608+
.Select(sourceFrom)//.LeftToRight(sourceFromeImage, 20).CenterYTo(sourceFromeImage)
559609
.Select(title).LeftToLeft(null, 20).RightToRight(null, 20).BottomToBottom(null, 20).Width(SizeBehavier.MatchConstraint);
560610

561611
var bigWindow = new FluentConstraintSet();
562612
bigWindow.Clone(layout);
563613
bigWindow
564-
.Select(guideLine, image, sourceFromeImage, sourceFrom, title).Clear()//需要移除之前的约束
565-
.Select(image).RightToRight(null, 20).TopToTop(null, 20)
566-
.Width(140).Height(140)
567-
.Select(sourceFromeImage).LeftToLeft(null, 20).TopToTop(image)
568-
.Width(SizeBehavier.WrapContent).Height(SizeBehavier.WrapContent)
569-
.Select(sourceFrom).LeftToRight(sourceFromeImage, 20).CenterYTo(sourceFromeImage)
570-
.Select(title).LeftToLeft(null, 20).RightToLeft(image, 20).TopToBottom(sourceFromeImage, 20).Width(SizeBehavier.MatchConstraint);
571-
614+
.Select(guideLine,
615+
//image, sourceFromeImage,
616+
sourceFrom, title).Clear()//需要移除之前的约束
617+
//.Select(image).RightToRight(null, 20).TopToTop(null, 20).Width(140).Height(140)
618+
//.Select(sourceFromeImage).LeftToLeft(null, 20).TopToTop(image).Width(SizeBehavier.WrapContent).Height(SizeBehavier.WrapContent)
619+
.Select(sourceFrom)//.LeftToRight(sourceFromeImage, 20).CenterYTo(sourceFromeImage)
620+
.Select(title).LeftToLeft(null, 20)//.RightToLeft(image, 20).TopToBottom(sourceFromeImage, 20).Width(SizeBehavier.MatchConstraint);
621+
;
572622
double oldValue = -1;
573623
layout.SizeChanged += (sender, e) =>
574624
{

SharpConstraintLayout.Maui.Example/Pages/MainPage.xaml.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ ConstraintLayout CreateConstraintLayout()
7373
ConstrainPaddingBottom = 10,
7474
ConstrainPaddingLeft = 10,
7575
ConstrainPaddingRight = 10,
76+
DebugName = "Root",
7677
Background = new SolidColorBrush(Colors.HotPink),
7778
};
7879
gridLayout.RemoveAt(gridLayout.Count - 1);

SharpConstraintLayout.Maui.Native/Helper/Widget/Flow.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,10 @@ public override void onMeasure(androidx.constraintlayout.core.widgets.VirtualLay
185185
if (ConstraintLayout.DEBUG) Debug.WriteLine(TAG, $"widthMode {widthMode}, widthSize {widthSize}, heightMode {heightMode}, heightSize {heightSize}");
186186
layout.measure(widthMode, widthSize, heightMode, heightSize);
187187
#if __MAUI__
188-
this.WidthRequest = layout.Width;
189-
this.HeightRequest = layout.Height;
188+
//this.WidthRequest = layout.Width;
189+
//this.HeightRequest = layout.Height;给WidthRequest赋值造成测量循环
190+
191+
MeasuredSize = new Size(layout.MeasuredWidth, layout.MeasuredHeight);
190192
#elif __ANDROID__ && !__MAUI__
191193
SetMeasuredDimension(layout.MeasuredWidth, layout.MeasuredHeight);//Android中这个的作用应该是设置flow的大小
192194
#elif __IOS__ && !__MAUI__

SharpConstraintLayout.Maui.Native/Widget/ConstraintLayout.cs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public partial class ConstraintLayout : Panel, IConstraintLayout
7878
/// if is true,will print some layout info.
7979
/// </summary>
8080
public static bool DEBUG = false;
81+
public string DebugName;
8182
/// <summary>
8283
/// if is true,will print time of measure spend.
8384
/// </summary>
@@ -335,7 +336,9 @@ public int ConstrainMaxHeight
335336
}
336337

337338
/// <summary>
338-
/// 约束自身的大小,可能会与父布局产生冲突
339+
/// 该属性约束自身的高度,其值可以是固定数值,或者<see cref="ConstraintSet.WrapContent"/>,<see cref="ConstraintSet.MatchConstraint"/>,<see cref="ConstraintSet.MatchParent"/>,
340+
/// 当依赖Parent给与的高度时,使用<see cref="ConstraintSet.MatchParent"/>,其为默认;当依赖自身Child的大小时,使用其它.
341+
/// (因为父布局的行为不能确定,因此可能会与父布局产生冲突,请多加尝试)
339342
/// </summary>
340343
public int ConstrainHeight
341344
{
@@ -358,6 +361,11 @@ public int ConstrainHeight
358361
}
359362
}
360363

364+
/// <summary>
365+
/// 该属性约束自身的宽度,其值可以是固定数值,或者<see cref="ConstraintSet.WrapContent"/>,<see cref="ConstraintSet.MatchConstraint"/>,<see cref="ConstraintSet.MatchParent"/>,
366+
/// 当依赖Parent给与的宽度时,使用<see cref="ConstraintSet.MatchParent"/>,其为默认;当依赖自身Child的大小时,使用其它.
367+
/// (因为父布局的行为不能确定,因此可能会与父布局产生冲突,请多加尝试)
368+
/// </summary>
361369
public int ConstrainWidth
362370
{
363371
get
@@ -383,11 +391,14 @@ public int ConstrainWidth
383391
#endregion LayoutProperty
384392

385393
#region Measure
394+
395+
/*@zhouyang 2022/6/13 注释掉.
386396
/// <summary>
387397
/// 存储MeasureSpec给子ConstraintLayout使用,因为iOS和Windows布局体系里本身不传递
388398
/// </summary>
389399
public int HorizontalSpec { get; set; } = 0;
390400
public int VerticalSpec { get; set; } = 0;
401+
*/
391402

392403
/// <summary>
393404
/// Android中Spec由parent制作,其它平台需自己制作
@@ -477,7 +488,12 @@ public int ConstrainWidth
477488
verticalSpec = AndroidMeasureSpec.MakeMeasureSpec(constrainHeight, AndroidMeasureSpec.EXACTLY);
478489
}
479490

480-
if (!isInfinityAvailabelWidth && constrainWidth <= 0)
491+
/*
492+
* @zhouyang 2022/6/13 注释掉以下与HorizontalSpec相关代码.
493+
* 在处理嵌套ConstraintLayout时,如果指定了子ConstraintLayout的大小,那么Measure时传入ConsraintLayout的值应该是确定的,
494+
* 如果再使用Parent的度量值去计算,那么大小会不正确.
495+
*/
496+
/*if (!isInfinityAvailabelWidth && constrainWidth <= 0)
481497
{
482498
if (this.GetParent() is ConstraintLayout)
483499
{
@@ -503,21 +519,20 @@ public int ConstrainWidth
503519
else
504520
if (DEBUG) SimpleDebug.WriteLine($"{parent} verticalSpec is 0");
505521
}
506-
507-
}
522+
}*/
508523

509524
isInfinityAvailabelWidth = false;
510525
isInfinityAvailabelHeight = false;
511526

512-
//存储Spec给Child使用
527+
/*//存储Spec给Child使用
513528
if (horizontalSpec != HorizontalSpec)
514529
{
515530
HorizontalSpec = horizontalSpec;
516531
}
517532
if (verticalSpec != VerticalSpec)
518533
{
519534
VerticalSpec = verticalSpec;
520-
}
535+
}*/
521536

522537
return (horizontalSpec, verticalSpec);
523538
}
@@ -1107,7 +1122,9 @@ public void AndroidSourceCodeMeasureUseSpecForWindows(ConstraintWidget widget, B
11071122
((VirtualLayout)child).onMeasure(layout, horizontalSpec, verticalSpec);
11081123
if (DEBUG) SimpleDebug.WriteLine($"{child.GetType().FullName} after onMeasure: widget={widget},control={child.GetViewLayoutInfo()}");
11091124
#if __MAUI__
1110-
(w, h) = ((int)child.WidthRequest, (int)child.HeightRequest);
1125+
//(w, h) = ((int)child.WidthRequest, (int)child.HeightRequest);
1126+
var size = (child as VirtualLayout).MeasuredSize;
1127+
(w, h) = ((int)size.Width, (int)size.Height);
11111128
#elif __IOS__ && !__MAUI__
11121129
(w, h) = ((int)child.Bounds.Width, (int)child.Bounds.Height);//我在iOS的Flow中,将测量值存储到了Bounds
11131130
#elif WINDOWS && !__MAUI__

SharpConstraintLayout.Maui.Native/Widget/VirtualLayout.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ protected override void init()
6060
base.init();
6161
}
6262

63+
#if __MAUI__
64+
public Size MeasuredSize;//创建这个变量专门存储Widget测量值
65+
#endif
6366
public virtual void onMeasure(androidx.constraintlayout.core.widgets.VirtualLayout layout, int widthMeasureSpec, int heightMeasureSpec)
6467
{
6568
// nothing

SharpConstraintLayout.Maui/SharpConstraintLayout.Maui.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
1616
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
1717
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
18-
<Version>2.1.1.4</Version>
18+
<Version>2.1.1.5</Version>
1919
<Description>constraintlayout for maui</Description>
2020
<Copyright></Copyright>
2121
<PackageProjectUrl>https://github.com/xtuzy/SharpConstraintLayout</PackageProjectUrl>

0 commit comments

Comments
 (0)