简体   繁体   English

如何使用 Xamarin 表单中的滚动菜单制作许多内容页面的选项卡式页面?

[英]How to make Tabbed page of many content page with a scroll menu in Xamarin forms?

I want to make a tabbed page with 7 content page (in xamarin forms, NOT in native project).我想制作一个包含 7 个内容页面的标签页(以 xamarin 形式,而不是在本机项目中)。 but the menu bar in red (I don't know what this thing is called so I call it menu bar) is not a scroll menu, so the title of each content page is not shown well.但是红色的菜单栏(我不知道这个东西叫什么所以我叫它菜单栏)不是滚动菜单,所以每个内容页的标题都没有很好地显示出来。 Like this:像这样:

the thing that I actually have我真正拥有的东西
我真正拥有的东西

Somebody knows to make something like this?有人知道做这样的东西吗?

thing that I want it to be我希望它成为的东西
我希望它成为的东西

Well cannot say much without seeing some code!没有看到一些代码就不能说太多! - but my assumption is that your problem is with your theme... - 但我的假设是你的问题出在你的主题上......

Open up your 'Tabbar.axml' and change this line of code:打开你的“Tabbar.axml”并更改这行代码:

app:tabMode="fixed" 

To:至:

app:tabMode="scrollable"

UPDATE:更新:

Then simply add the new line app:tabMode="scrollable" because by default is "fixed"然后只需添加新行app:tabMode="scrollable"因为默认情况下是“固定的”

Anyways as you requested here is my Tabbar.axml :无论如何,你在这里要求的是我的 Tabbar.axml :

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.TabLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/sliding_tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimaryDark"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:tabIndicatorColor="@android:color/white"
    app:tabGravity="fill"
    app:tabMode="scrollable" />

You can also change this by creating a CustomRednerer.您还可以通过创建 CustomRednerer 来更改此设置。 My solution is good if you want to create many tabbed pages in your application and you want to make one of them with scrollable tabs and second with non-scrollable tabs.如果您想在您的应用程序中创建许多选项卡式页面,并且您想让其中一个带有可滚动选项卡,第二个带有不可滚动选项卡,我的解决方案是很好的。

Here is code for Droid project:这是 Droid 项目的代码:

using Android.Support.Design.Widget;
using App1;
using App1.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android.AppCompat;

[assembly: ExportRenderer(typeof(ScrollableTabbedPage), typeof(ScrollableTabbedPageRenderer))]
namespace App1.Droid
{
    public class ScrollableTabbedPageRenderer : TabbedPageRenderer
    {
        public override void OnViewAdded(Android.Views.View child)
        {
            base.OnViewAdded(child);
            var tabLayout = child as TabLayout;
            if (tabLayout != null)
            {
                tabLayout.TabMode = TabLayout.ModeScrollable;
            }
        }

    }
}

For Portable project:对于便携式项目:

using System;
using Xamarin.Forms;

namespace App1
{
    public class ScrollableTabbedPage : TabbedPage
    {
    }

    public class App : Application
    {
        public App()
        {
            var tabbedPage = new ScrollableTabbedPage();
            for (int i = 0; i < 7; i++)
            {
                tabbedPage.Children.Add(this.GetMyContentPage(i));
            }

            MainPage = new NavigationPage(tabbedPage);
        }

        private ContentPage GetMyContentPage(int i)
        {
            return new ContentPage
            {
                Title = "Tab number " + i,
                Content = new StackLayout
                {
                    Children = {
                        this.GetMyButton()
                    }
                }
            };
        }

        private Button GetMyButton()
        {
            var myButton = new Button()
            {
                Text = "Welcome to Xamarin Forms!",
            };

            myButton.Command = new Command(() =>
            {
                myButton.Text = "Start" + DateTime.Now.ToString();
            });
            return myButton;
        }
    }
}

And for result you get this:结果你得到这个:

带有可滚动标签的页面

@Paweł Kanarek Your code for Droid project works perfectly. @Paweł Kanarek 您的 Droid 项目代码运行良好。 There's only a change needed to solve a build error.只需进行更改即可解决构建错误。

Change the line:更改行:

[assembly: ExportRenderer(typeof(ScrollableTabbedPage), typeof(ScrollableTabbedPageRenderer))]

to:到:

[assembly: ExportRenderer(typeof(TabbedPage), typeof(App1.Droid.ScrollableTabbedPageRenderer))]

Thanks for your solution.感谢您的解决方案。 It helped me perfectly.它完美地帮助了我。

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

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