2016-12-04 110 views
0

如何更改標籤標題的樣式?最重要的是,標題不會像截圖一樣截斷。所以我必須改變標題的大小。 我開始爲tabbedpage創建自定義渲染器,但我不知道如何繼續。Xamarin Forms Android:TabbedPage字體樣式

Xaml: 
<custom:CustomTabbedPage... 

Forms: 
public class CustomTabbedPage : TabbedPage... 

Forms.Droid 
public class CustomTabbedPageRenderer : TabbedPageRenderer 

TabbedPage具有帶有ContentPages的NavigationPages。

如果您需要更多信息,請告訴我。謝謝。

enter image description here

<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms" 
     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
     xmlns:local="clr-namespace:TabbedPageWithNavigationPage;assembly=TabbedPageWithNavigationPage" 
     x:Class="TabbedPageWithNavigationPage.MainPage"> 
<NavigationPage Title="Start" Icon="start.png"> 
    <x:Arguments> 
     <local:StartPage /> 
    </x:Arguments> 
</NavigationPage> 
<NavigationPage Title="Symptom-Tagebuch" Icon="tagebuch.png"> 
    <x:Arguments> 
     <local:TagebuchPage /> 
    </x:Arguments> 
</NavigationPage> 
... 

+0

沒人? 因此,所有人都對切割標題感到滿意,沒有人改變過它。不能相信這一點。 ;) – Ralf

+0

你能不能展示一些代碼,你是如何爲'TabbedPage'創建每個項目的?我使用它的'ItemTemplate'來構建多個'ContentPage'的源集合,並通過'ContentPage'的'Title'屬性指定每個項目的標題,它工作正常,標題不會被切斷,所有樣式都是默認的,所以我無法重現您的問題。 –

+0

@ GraceFeng-MSFT舒爾,我添加了一些代碼從我的項目 – Ralf

回答

1

看來,對不同尺寸的設備的不同NavigationPage行爲默認的標題樣式。通過我對7" 側奇巧模擬器,所以它看起來如此:

enter image description here

和5" 奇巧模擬器,所以它看起來如此:

enter image description here

或者它可能是版本的問題,這導致我的身邊這個NavigationPage的行爲與你的不同,我無法重現你的問題。無論如何,如果你想自定義你的NavigationPage的佈局,你可以爲你的android平臺創建一個自定義渲染。欲瞭解更多信息,你可以參考官方文檔Customizing Controls on Each Platform,如果你正在尋找演示,在官方論壇上有關於定製標題字體NavigationPage的討論,你也可以看看:Discussion 1Discussion 2

另一種可能的解決問題的方法是,我認爲你可以改變NavigationPageContentPage,並改變你的子頁面內容視圖,並通過這樣做,你可以參考Xamarin.Forms: Can I embed one ContentPage or ContentView into another ContentPage

根據您的描述,也許通過創建自己的自定義渲染的第一個解決方案更適合您的方案。

+0

謝謝你,我發現了另一個解決方案,並且這些天會發布 – Ralf

+0

@Ralf你發現了什麼?謝謝! –

0

你可以找到標題的通過TabLayout視圖孩子CustomRendered TextView的,改變字體樣式

[assembly: ExportRenderer(typeof(MainPage), typeof(BottomTabbedPageRenderer))] 
namespace Daddy.Droid 
{ 
    public class BottomTabbedPageRenderer : TabbedPageRenderer 
    { 
     protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e) 
     { 
      base.OnElementChanged(e); 

      if (e.NewElement == null || e.OldElement != null) 
       return; 

      TabLayout tablayout = (TabLayout)ViewGroup.GetChildAt(1); 
      Android.Views.ViewGroup vgroup = (Android.Views.ViewGroup)tablayout.GetChildAt(0); 
      for (int i = 0; i < vgroup.ChildCount; i++) 
      { 
       Android.Views.ViewGroup vvgroup = (Android.Views.ViewGroup)vgroup.GetChildAt(i); 
       Typeface fontFace = Typeface.CreateFromAsset(this.Context.Assets, "IranSans.ttf"); 
       for (int j = 0; j < vvgroup.ChildCount; j++) 
       { 
        Android.Views.View vView = (Android.Views.View)vvgroup.GetChildAt(j); 
        if (vView.GetType() == typeof(Android.Support.V7.Widget.AppCompatTextView) || vView.GetType() == typeof(Android.Widget.TextView)) 
        { 
         //here change textview style 
         TextView txtView = (TextView)vView;       
         txtView.TextSize = 14f; 
         txtView.SetTypeface(fontFace, TypefaceStyle.Normal); 
        } 
       } 
      } 
     } 
    } 
}