如何在 xamarin 窗体中将导航图标替换为导航标题



我尝试在其中一个页面中向我的应用程序添加导航标题图标,我使用的代码是:

对于 iOS:

NavigationPage.SetTitleIcon(this, "icon.png");

对于 ToolBar.axml 中的 Android,我添加了一个 ImageView,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<ImageView
android:id="@+id/titleicon"
android:src="@drawable/UstNameIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>

问题是,我希望我的导航图标在下一个页面中被禁用,并放置标题而不是图标。

为此,我尝试了

对于 iOS:

NavigationPage.SetTitleIcon(this, null);

NavigationPage.Title="Title";

对于安卓,如何实现导航标题的更改?

In MainActivity.cs

[Activity (Label = "Todo.Android.Android", MainLauncher = true, Icon="@android:color/transparent")]

对于标题,可以在 xaml 或 c# 中设置Title属性

要更改文本,您只需通过id找到该元素,并设置标题:

var toolbar = 
FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar); // it may be simplifed
SetSupportActionBar(toolbar);
toolbar.Title = "title";

并且图像在toolbar.Logo下,所以您可以尝试:

toolbar.Logo = null;

如果要放入的资源中有一些数据,则还可以使用:

toolbar.SetTitle();
toolbar.SetLogo();

最新更新