Xamarin表单中的等宽+ TailTruncation

时间:2017-09-29 13:57:40

标签: xaml xamarin.forms

我正在尝试使用4个相等宽度的列构建列表视图。 我正在尝试Horizo​​ntalOptions =" FillAndExpand"为此,除非标签文字宽于屏幕宽度的1/4,否则它有效。 有没有办法让标签上行以适应1/4屏幕? 我唯一的想法是在标题和每一行中分别使用固定大小的网格。 这是XAML:

            <ListView ItemsSource="{Binding Members}">
            <ListView.Header>
                <StackLayout Orientation="Horizontal">
                    <Label Text="Experience Level"/>
                    <Label Text="Years of Experience"/>
                    <Label Text="Max Hourly Rate"/>
                    <Label Text="Professional/Consultant"/>
                </StackLayout>
            </ListView.Header>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Horizontal">
                            <Label Text="{Binding ExperienceLevelType.Name}" LineBreakMode="TailTruncation"  HorizontalOptions="FillAndExpand"/>
                            <Label Text="{Binding YearsOfExperience}" HorizontalOptions="FillAndExpand"/>
                            <Label Text="{Binding HourlyRateMax}" HorizontalOptions="FillAndExpand"/>
                            <Button Text="Search" HorizontalOptions="FillAndExpand"/>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

screenshot

1 个答案:

答案 0 :(得分:1)

而不是Grid,建议使用AbsoluteLayout

<ListView x:Name="listView" ItemsSource="{Binding Members}">
    <ListView.Header>
        <AbsoluteLayout HeightRequest="50" BackgroundColor="Silver">
            <Label
                AbsoluteLayout.LayoutBounds="0,0,.25,1" AbsoluteLayout.LayoutFlags="All" 
                HorizontalOptions="Start" VerticalOptions="Center"
                LineBreakMode="WordWrap" Text="Experience Level"/>
            <Label
                AbsoluteLayout.LayoutBounds=".325,0,.25,1" AbsoluteLayout.LayoutFlags="All" 
                HorizontalOptions="Center" VerticalOptions="Center"
                LineBreakMode="WordWrap" Text="Years of Experience"/>
            <Label
                AbsoluteLayout.LayoutBounds=".65,0,.25,1" AbsoluteLayout.LayoutFlags="All" 
                HorizontalOptions="Center" VerticalOptions="Center"
                LineBreakMode="WordWrap" Text="Max Hourly Rate"/>
            <Label
                AbsoluteLayout.LayoutBounds="1,0,.25,1" AbsoluteLayout.LayoutFlags="All"
                HorizontalOptions="Start" VerticalOptions="Center"
                LineBreakMode="WordWrap" Text="Professional/Consultant"/>
        </AbsoluteLayout>
    </ListView.Header>
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <AbsoluteLayout>
                    <Label 
                        AbsoluteLayout.LayoutBounds="0,0,.25,1" AbsoluteLayout.LayoutFlags="All" 
                        HorizontalOptions="Start" VerticalOptions="Center"
                        LineBreakMode="TailTruncation" Text="{Binding ExperienceLevelType.Name}"/>
                    <Label
                        AbsoluteLayout.LayoutBounds=".325,0,.25,1" AbsoluteLayout.LayoutFlags="All"
                        HorizontalOptions="Center" VerticalOptions="Center"
                        LineBreakMode="TailTruncation" Text="{Binding YearsOfExperience}" />
                    <Label
                        AbsoluteLayout.LayoutBounds=".65,0,.25,1" AbsoluteLayout.LayoutFlags="All" 
                        HorizontalOptions="Center" VerticalOptions="Center"
                        LineBreakMode="TailTruncation" Text="{Binding HourlyRateMax}" />
                    <Button
                        AbsoluteLayout.LayoutBounds="1,0,.25,1" AbsoluteLayout.LayoutFlags="All"
                        HorizontalOptions="Start" VerticalOptions="Center"
                        Text="Search" />
                </AbsoluteLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>