如何使导航栏文本加粗?

时间:2018-12-20 19:44:00

标签: xamarin.forms xamarin.uwp

我有一个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;
    }

3 个答案:

答案 0 :(得分:1)

在这里给了我一个解决方案:

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);

答案 1 :(得分:0)

对于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"/>

答案 2 :(得分:0)

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

放入

LoadApplication(new App());之前