Xamarin NavigationView 菜单项自定义

Xamarin NavigationView menu item customization

你好,我使用 C# 和 Xamarin 开发我的新应用程序,运行 使用这个平台遇到很多问题。是否可以使用 xamarin 在 NavigationView 中自定义菜单项?我找到的只有 valid properties for the menu items。 但是如果不是这样呢:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
      android:id="@+id/itemOne"
      android:title="Go to page one" />
    <item
      android:id="@+id/itemTwo"
      android:title="Go to page two" />
</menu>

我想这样做:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView></TextView>
    <Button></Button>
    <SomeOtherControlHere></SomeOtherControlHere>
</menu>

是否可以使用 xamarin?我的其他选择是什么?

除此之外,我发现 Xamarin API 的限制非常多,例如,您不能直接在 xml 中使用自定义 ttf 字体来设置菜单项的字体:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item
          android:id="@+id/itemOne"
          android:title="Go to page one" 
          android:fontFamily="MyCustomFontName Or MyPathToFonts" /> <-- NOT VALID
</menu>

此外,如果我为整个菜单项容器应用样式,也无法使用自定义字体,我们唯一可以使用的字体是内置字体...

即使我在 Activity 中获得对单个菜单项 (IMenuItem) 的引用,也没有 属性 或设置字体系列的函数。

可以定义更复杂的 NavigationView。只需将子控件放在 NavigationView-Tag 中。例如,这就是我使用 NavigationView 实现页脚的方式。子控件没有限制,因此您也可以使用按钮等:

<android.support.design.widget.NavigationView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:id="@+id/navigation_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    local:headerLayout="@layout/drawer_header"
    local:theme="@style/NavigationDrawerStyle"
    local:menu="@menu/drawer_menu">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="bottom"
        android:orientation="vertical">
        <TextView
            android:id="@+id/empty_spacer"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:clickable="false"
            android:text="" />
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="65dp"
            android:background="@drawable/footer_small"
            android:orientation="vertical"
            android:weightSum="1" />
    </LinearLayout>
</android.support.design.widget.NavigationView>

例如,要更改项目的 TextSize,您必须定义自己的样式:

<style name="NavigationDrawerStyle">
   <item name="android:textSize">24sp</item>
   <!-- more properties -->
</style>

然后将其分配给您的 NavigationView:

<android.support.design.widget.NavigationView app:theme="@style/NavigationDrawerStyle">
   <!-- ... -->
</android.support.design.widget.NavigationView>

这就是我格式化导航菜单项的方式,它以图标开头,然后是菜单文本,最后一个是数字。它看起来像 gmail 应用程序中的那个。

创建时

navigationView = FindViewById<NavigationView>(Resource.Id.wnl_nav_view);

            //Add customize menu item style
            IMenu menuNav = navigationView.Menu;
            View view = View.Inflate(this, Resource.Layout._drawerMenuItemTemplate, null);
            IMenuItem logoutItem = menuNav.FindItem(Resource.Id.nav_mail);
            MenuItemCompat.SetActionView(logoutItem, view);
            TextView txtNewNumber = view.FindViewById<TextView>(Resource.Id.txtNewNumber);

_drawerMenuItemTemplate.axml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/primary_text"
        android:text="3/30"
        android:id="@+id/txtNewNumber"
        android:layout_gravity="right"
        android:fontFamily="@string/abc_font_family_display_2_material"
        android:textSize="13sp"
        android:textStyle="bold"
        android:paddingRight="6dp"
        android:layout_marginTop="14dp" />
</FrameLayout>