使用 Xamarin.Forms,如何实现这样的布局?
我说的是灰色背景上的文字提示:
Turn on Personal Hotspot to share ...
所有我可以添加到 ListView
或 TableView
的都是标准的 ViewCells
,它们都使用分隔符和白色背景呈现。
如果使用 TableView
或 ListView
无法实现此布局,我该如何呈现 ViewCell
(如 Wi-Fi 密码示例图像中的一个)在标准 StackLayout
中?
最佳答案
即使 TableView 没有开箱即用的 ViewCell
可以做到这一点,您当然可以推出自己的实现。显然有一个 TextCell,但它不允许您指定字体大小和其他内容。
你可以这样做:
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="DemoTableView.TablePage" Title="TableView">
<ContentPage.Content>
<TableView Intent="Settings">
<TableRoot>
<TableSection>
<SwitchCell Text="Personal Hotspot" />
<ViewCell> <!-- Simple text -->
<Label Text="Turn on personal hotspot..." FontSize="Small" ForegroundColor="Gray" />
</ViewCell>
<ViewCell> <!-- More complex cell -->
<StackLayout Orientation="Horizontal>
<Image Source="someicon" />
<StackLayout>
<Label Text="TO CONNECT USING WIFI" FontSize="Large"/>
<Label Text="Something more" FontSize="Small" />
</StackLayout>
</StackLayout>
</ViewCell>
</TableSection>
</TableRoot>
</TableView>
</ContentPage.Content>
</ContentPage>
这只是一个匆忙写的简单例子,但你明白了。您还可以通过从 ViewCell
类继承并在 TableView 中显示它们,以编程方式定义自己的单元格。
Here's more关于官方文档中的自定义单元格。
编辑:根据您的评论,我们需要深入研究平台特定代码以禁用对纯文本单元格的选择。您应该使用以下适用于 iOS 的自定义渲染器创建自定义 ViewCell 实现:
[assembly: ExportCell(typeof(YourCell), typeof(YourCellRenderer))]
namespace YourProject.iOS
{
public class YourCellRenderer : ViewCellRenderer
{
public override UITableViewCell GetCell (Cell item, UITableView tv)
{
var cell = base.GetCell (item, tv);
cell.SelectionStyle = UITableViewCellSelectionStyle.None;
return cell;
}
}
}
关于ios - 如何将这些自定义单元格添加到 TableView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46000569/