首页 > 解决方案 > 为 Xamarin TableView 中的每个 EntryCell 设置相同的标签宽度

问题描述

我正在学习如何<TableView>在 Xamarin Forms 中使用,我写了这个:

<TableView Intent="Form">
    <TableRoot>
        <TableSection Title="Random values">
            <EntryCell Label="A beautiful first number" Placeholder="1" Keyboard="Numeric"/>
            <EntryCell Label="2nd num" Placeholder="2" Keyboard="Numeric"/>
        </TableSection>
    </TableRoot>
</TableView>

如何为每个设置相同的宽度<entry cell>?我不希望输入部分从不同的水平位置开始。

标签: xamarin.forms

解决方案


据我所知,Custom Cell将是实现这一目标的不错选择。因为Entry Cell没有设置Width的属性Label

使用 View Cell 可以包含布局以满足您的需求,如下所示:

<TableView Intent="Form" >
    <TableRoot>
        <TableSection Title="Random values">
            <EntryCell Label="A beautiful first number"
                        Placeholder="1 Entry Cell"
                        Keyboard="Numeric" />
            <EntryCell Label="2nd num"
                        Placeholder="2 Entry Cell"
                        Keyboard="Numeric" />
            <ViewCell>
                <StackLayout Orientation="Horizontal"
                                Padding="2">
                    <Label Text="A beautiful first number"
                            WidthRequest="150" 
                            Margin="5,0,0,0"
                            VerticalOptions="CenterAndExpand"/>
                    <Entry Placeholder="1 View Cell" WidthRequest="250"/>
                </StackLayout>
            </ViewCell>
            <ViewCell>
                <StackLayout Orientation="Horizontal" Padding="2" >
                    <Label Text="2nd num"
                            WidthRequest="150"
                            Margin="5,0,0,0"
                            VerticalOptions="CenterAndExpand" />
                    <Entry Placeholder="2 View Cell" 
                            WidthRequest="250" />
                </StackLayout>
            </ViewCell>
        </TableSection>
    </TableRoot>
</TableView>

效果:

在此处输入图像描述


推荐阅读