Xamarin.Forms:StackLayout内的ListView:如何设置高度? [英] Xamarin.Forms: ListView inside StackLayout: How to set height?
问题描述
在ContentPage
中,我在ScrollView
内的StackLayout
内有ListView
.当调用OnAppearing
时,在ContentPage
中填充了ListView
(设置了ItemSource
),我可以看到该列表已在模拟器中填充. StackLayout
的方向是Vertical
,在ListView
之下,我有Button
.
In a ContentPage
I have a ListView
inside a StackLayout
inside a ScrollView
. The ListView
is populated (ItemSource
is set) in the ContentPage
when OnAppearing
gets called and I can see that the list is populated in the emulator. The StackLayout
s orientation is Vertical
and below the ListView
I have a Button
.
我的问题是,无论ListView
包含多少个元素,它的高度都为53.33.我希望ListView
的高度与其中项目的总高度相匹配.通过设置HeightRequest
,我可以将ListView
的高度设置为我想要的任何值,但是由于我不知道ListView
内项目的高度,因此结果通常是到其下方按钮的距离为不正确,因此看起来很丑.我已经尝试将ListView
和StackLayout
上的VerticalOptions
都设置为Start
和其他设置,但是这不会更改53.33的高度(如果我尝试结合使用HeightRequest
和
My problem is that no matter how many elements the ListView
has, it gets the height of 53.33. I would like the height of the ListView
to match the total height of the items in it. By setting HeightRequest
I can set the height of the ListView
to anything I want, but since I do not know the height of the items inside the ListView
the result is most often that the distance to the button below it is incorrect and therefore looks ugly. I have tried to set VerticalOptions
on both the ListView
and the StackLayout
to Start
and other settings, but this does not change the height from 53.33 (and if I try to combine using HeightRequest
and Start
it turns out that HeightRequest wins out).
我该如何解决?
(请原谅来自 Xamarin论坛)
推荐答案
在我的案例中,解决方案是将ListView
放在StackLayout
内,然后将StackLayout
放在主StackLayout
内.然后,我可以在内部StackLayout
(包含ListView
的那个)上设置VerticalOptions = LayoutOptions.FillAndExpand
,结果是ListView
获得了所需的空间(当然,该空间根据数据而有所不同).
The solution in my case was to put the ListView
inside a StackLayout
and then put that StackLayout
inside the main StackLayout
. Then I could set the VerticalOptions = LayoutOptions.FillAndExpand
on the inner StackLayout
(the one containing the ListView
) with the result that the ListView
got the space it needed (which of course varies depending on the data).
这是主要代码:
listView.ItemsSource = alternativeCells;
listView.ItemSelected += ListViewOnItemSelected;
var listStackLayout = new StackLayout
{
VerticalOptions = LayoutOptions.FillAndExpand,
Orientation = StackOrientation.Vertical
};
listStackLayout.Children.Add(listView);
_stackLayout.Children.Add(listStackLayout);
如您所见,我添加了一个新的StackLayout
,其唯一目的是将ListView
放入其中.然后,将listStackLayout
放入主_stackLayout
中.
As you see, I added a new StackLayout
with the only purpose of putting the ListView
inside it. Then I put that listStackLayout
inside the main _stackLayout
.
有关更多信息,请参见此Xamarin论坛帖子上的帖子
See the post on this Xamarin forum post for more information
这篇关于Xamarin.Forms:StackLayout内的ListView:如何设置高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!