我怎样才能使我的导航栏文本加粗?

How can I make my navigation bar text bold?

我有一个Master-Detail架构,我需要将详细页面的导航栏标题文本加粗。我怎样才能以最简单的方式做到这一点?

这是我的一些代码:

    private void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        var item = e.SelectedItem as MainMDPageMenuItem;
        if (item == null)
            return;

        item.ItemBorderColor = Color.Red; // Make a red frame around the selected item
        if (PreviouslySelectedItem != null)
        {
            PreviouslySelectedItem.ItemBorderColor = Color.FromHex("#00a8d5"); // Return the original color to the previously selected (now deselected) item
        }

        var page = (Page)Activator.CreateInstance(item.TargetType);
        page.Title = item.Title; // THIS IS THE TITLE I AM TALKING ABOUT

        Detail = new NavigationPage(page);
        IsPresented = false;

        MasterPage.ListView.SelectedItem = null;

        PreviouslySelectedItem = item;
    }

对于 Android 部分,您可以通过在 Resources\values\styles.xml 文件中添加以下内容来实现此功能:

<style name="ActionBar.nameText" parent="TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textSize">58sp</item> <-- Could delete this line
    <item name="android:textStyle">bold</item>
</style>

然后,将其添加到您的 Resources\layout\Toolbar.axml:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:companyApp="http://schemas.android.com/apk/res-auto"
    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" 
    companyApp:titleTextAppearance="@style/ActionBar.nameText"/>

对于IOS,您应该简单地执行此操作。在 IOS 项目的 FinishedLaunching 中,您需要将此 UINavigationBar.Appearance.TitleTextAttributes = new UIStringAttributes() { Font = UIFont.SystemFontOfSize(18.0f, UIFontWeight.Heavy) };

之前 LoadApplication(new App());

我在这里得到了解决方案:

https://forums.xamarin.com/discussion/comment/358994#Comment_358994

使用TitleView完成:

        var page = (Page)Activator.CreateInstance(item.TargetType);

        var titleView = new Label
        {
            Text = item.Title,
            FontAttributes = FontAttributes.Bold,
            TextColor = Color.White,
            BackgroundColor = Color.FromHex("#00a8d5")
        };

        NavigationPage.SetTitleView(page, titleView);