使用 MvvmCross 将图标添加到 TabLayout

Add icons to TabLayout using MvvmCross

我一直在看 MvvmCross 上的 Playground 项目 (https://github.com/MvvmCross/MvvmCross/tree/develop/Projects/Playground)

将片段加载到选项卡中的方法是按以下方式设置属性:

[MvxTabLayoutPresentation(TabLayoutResourceId = Resource.Id.tabs, ViewPagerResourceId = Resource.Id.viewpager, Title = "Tab 1", ActivityHostViewModelType = typeof(TabsRootViewModel))]
[MvxTabLayoutPresentation(TabLayoutResourceId = Resource.Id.tabs, ViewPagerResourceId = Resource.Id.viewpager, Title = "Tab 1", FragmentHostViewType = typeof(TabsRootBView))]
[Register(nameof(Tab1View))]
public class Tab1View : MvxFragment<Tab1ViewModel>

我的问题是,除了可以在 MvxTabLayoutPresentation 上指定的标题外,如何向每个选项卡添加和图标?

这不是开箱即用的,更多的是关于 TabLayout 定制。

你可以这样实现。

为您的选项卡布局项目创建一个 CustomView,myCustomTab.axml:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/txtTab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:textColor="#FFFFFF"
    android:textSize="12"
    android:textStyle="bold" />

然后在你的视图中有 TabLayout 你配置它在 OnCreate / OnCreateView:

var tabLayout = view.FindViewById<TabLayout>(Resource.Id.tabs);
var customTab = inflater.Inflate(Resource.Layout.myCustomTab, null);
customTab.Text = "MyText";
// this sets the icon above the text
customTab.SetCompoundDrawablesWithIntrinsicBounds(0, Resource.Drawable.my_icon, 0, 0);
tabLayout.GetTabAt(0).SetCustomView(customTab);

显然,您必须根据拥有的选项卡布局项执行此操作的次数。

此外,您还可以使用它向选项卡布局项添加任何自定义项。

来源(在 java 中):https://mobikul.com/make-custom-tabs-icons-android/