Xamarin表单 - 如何创建自定义呈现给Android上的TableSection页脚?
问题描述:
我想将默认的Android页脚添加到Xamarin Forms中tableview的table section中。我怎么能这样做呢?我相信我需要一个tableview自定义渲染器,但是我不确定过去做到这一点。Xamarin表单 - 如何创建自定义呈现给Android上的TableSection页脚?
答
关于您的其他问题Xamarin Forms - How to create custom render to give TableSection the default iOS Footer?,我不确定是要添加页脚到整个TableView
还是要添加到TableView
的各个TableSection
。
如果你想页脚添加到每个TableSection
,我建议只在每个TableSection
因为TableSection
可以有多个Cell
孩子定制TextCell
添加为页脚,但如果你想页脚添加到TableView
,可以为示例代码像这样:
public class TableViewWithFooterRenderer : TableViewRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<TableView> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
if (Control != null)
{
//Add footer to the whole TableView
var lv = Control as Android.Widget.ListView;
var footerview = ((LayoutInflater)Context.GetSystemService(Context.LayoutInflaterService))
.Inflate(Resource.Layout.TableViewFooter, null, false);
lv.AddFooterView(footerview);
}
}
}
}
我创建了页脚视图,因为您也可以使用其他视图。我的页脚的看法是这样的:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="7dp">
<TextView android:id="@+id/footer"
android:text="This is footer"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center"
android:layout_gravity="center" />
</LinearLayout>
更新:
TableView
是在Android平台rendered as ListView,因为在Android平台上没有本地方法直接添加页脚的ListView
每一个项目,因为我建议,我们可以手动添加一个定制的TextCell
或ViewCell
作为每个项目的页脚。正如您从TableSection的源代码可以看到的那样,它是一个ObservableCollection
对象。
例如:
<TableSection Title="Section 1 Title">
<TextCell Text="TextCell Text" Detail="TextCell Detail"></TextCell>
<ViewCell IsEnabled="False">
<Grid>
<Label Text="This is footer" />
</Grid>
</ViewCell>
</TableSection>
我没有定制的ViewCell
风格在这里,您可以尝试在它自己的风格。
这就近了。我想要一个页脚到每个tablesection,而不是整个tableview。看看你的答案,我希望能够通过膨胀footview并更改文本并将其添加到每个部分。我怎么能做到这一点?我希望它与iOS版本中的其他问题一样接近,其中每个节的默认iOS页脚都有不同的文本。 – SolidSnake4444
@ SolidSnake4444,是的,据我所知,但正如我所说,我们可以添加自定义单元格作为页脚的每个表部分,我更新了我的答案,android中没有本地方法来添加页脚到android平台中的每个tablesection。 –
我给它一个镜头只显示在android和uwp视图单元格,它似乎工作。然而,Android上的最后一个单元格有一个底部边框,我不知道如何摆脱这一点,但我足够接近。 – SolidSnake4444