简体   繁体   English

如何在Xamarin.Forms的TabbedPage的选项卡中放置按钮?

[英]How can I place a button in the the tabs of a TabbedPage in Xamarin.Forms?

在此处输入图片说明 I am using a Tabbed Page combined with a Master Detail Page for navigation in my Xamarin.Forms app. 我在我的Xamarin.Forms应用程序中使用了选项卡式页面和主详细信息页面相结合的导航。 Currently when a menu option is selected from the Master Detail Page a new tabbed page is added with the page's content. 当前,当从“母版详细信息页面”中选择菜单选项时,将添加一个新的选项卡式页面,其中包含页面的内容。 I want to place a button to close the tab in the title field of the tab. 我想放置一个按钮以关闭选项卡的标题字段中的选项卡。 Is this possible? 这可能吗? Currently I just have a button within the content page for the tab, but this is less than ideal. 目前,我仅在内容页面的选项卡中有一个按钮,但这并不理想。 I want it to be very web browser like. 我希望它非常像网络浏览器。 Thanks in advance! 提前致谢!

Edit: I have added the image . 编辑:我已经添加了图像 Basically, I just want to add an "X" button to the right of each item in the tab bar that would allow me to close that tab. 基本上,我只想在选项卡栏中每个项目的右侧添加一个“ X”按钮,即可关闭该选项卡。 Just like you would in Chrome or something. 就像您在Chrome浏览器中一样。

You can use custom renderer to create your custom TabbedPage in android platform. 您可以使用自定义渲染器在android平台中创建自定义TabbedPage Disagree with Yuri, on android we can add an image to tab, in fact we can customize the layout of tab. 与Yuri不同,在android上我们可以向tab添加图像,实际上我们可以自定义tab的布局。

Since in your image, I saw you didn't use the Icon property for each tab, I use this icon as a close button. 由于在您的图像中,我看到您没有为每个选项卡使用Icon属性,因此我将此图标用作关闭按钮。 But sure you can also not use this, it is self customized. 但请确保您也不能使用此功能,它是自定义的。

In PCL, create a MyTabbedPage : 在PCL中,创建一个MyTabbedPage

public class MyTabbedPage : TabbedPage
{
}

In Android platform create a renderer for it: 在Android平台中为其创建渲染器:

[assembly: ExportRenderer(typeof(MyTabbedPage), typeof(MyTabbedPageRenderer))]

namespace YOURNAMESPACE.Droid
{
    public class MyTabbedPageRenderer : TabbedPageRenderer
    {
        private ObservableCollection<Xamarin.Forms.Element> children;
        private IPageController controller;

        protected override void SetTabIcon(TabLayout.Tab tab, FileImageSource icon)
        {
            base.SetTabIcon(tab, icon);

            tab.SetCustomView(Resource.Layout.mytablayout);

            var imagebtn = tab.CustomView.FindViewById<ImageButton>(Resource.Id.closebtn);
            imagebtn.SetBackgroundDrawable(tab.Icon);

            var title = tab.CustomView.FindViewById<TextView>(Resource.Id.tabtitle);
            title.Text = tab.Text;

            imagebtn.Click += (sender, e) =>
            {
                var closebtn = sender as ImageButton;
                var parent = closebtn.Parent as Android.Widget.RelativeLayout;
                var closingtitle = parent.FindViewById<TextView>(Resource.Id.tabtitle);
                foreach (var child in children)
                {
                    var page = child as ContentPage;
                    if (page.Title == closingtitle.Text)
                    {
                        children.Remove(child);
                        break;
                    }
                }
            };
        }

        protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
        {
            base.OnElementChanged(e);
            if (e.NewElement != null)
            {
                controller = Element as IPageController;
                children = controller.InternalChildren;
            }
        }
    }
}

Use it like this: 像这样使用它:

<local:MyTabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:TabbedPageForms"
             x:Class="TabbedPageForms.MainPage">

    <local:TodayPage Title="Today" Icon="hamburger.jpg" />

    <local:SchedulePage Title="Schedule" Icon="hamburger.jpg" />
</local:MyTabbedPage>

Code behind, don't forget to change MainPage to inherit from MyTabbedPage : 后面的代码,不要忘记更改MainPage以从MyTabbedPage继承:

public partial class MainPage : MyTabbedPage
{
    public MainPage()
    {
        InitializeComponent();
    }
}

在此处输入图片说明

Please pay attention here , if you look closer to my code, you will find that I used Title of each tab for the comparing and removing the matching item, it will find the first matched title and remove the page of that title. 请注意这里 ,如果您靠近我的代码,会发现我使用每个选项卡的Title来比较和删除匹配项,它将找到第一个匹配的标题并删除该标题的页面。 This may cause a problem if you have several tabs with the same title. 如果您有多个标题相同的选项卡,则可能会导致问题。 This is a potential bug of this demo, you may try to solve it. 这是该演示的潜在错误,您可以尝试解决。

Update: 更新:

Forgot to post the code of mytablayout , here is it: 忘记发布mytablayout的代码了,就是这样:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  <TextView android:id="@+id/tabtitle"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center_horizontal" />

  <ImageButton
    android:id="@+id/closebtn"
    android:layout_height="30dp"
    android:layout_width="30dp"
    android:scaleType="fitCenter"
    android:layout_alignParentRight="true"
    android:gravity="center" />
</RelativeLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM