问题
I'm using a TabbedPage as my MainPage of my Xamarin.Forms app (Xamarin.Forms Version: 2.3.5.239-pre3). My MainActivity inherits from FormsAppCompatActivity.
There are four pages of type ContentPage added to the TabbedPage like:
<TabbedPage ... >
<pages:FirstPage Title="Testpage1" Icon="icon.png" />
<pages:SecondPage Title="Testpage2" Icon="icon.png" />
<pages:ThirdPage Title="Testpage3" Icon="icon.png" />
<pages:FourthPage Title="Testpage3" Icon="icon.png" />
</TabbedPage>
However, the tabs are displayed like:
Now I need to change the font size of the title property, so the whole title will be displayed. Whats the best way to do this? I tried a CustomRenderer but I couldn't figure out how to access the tab-items.
I tried:
[assembly: ExportRenderer(typeof(TabbedPage), typeof(CustomTab))]
namespace AdvancedForms.Droid.CustomRenderer
{
public class CustomTab : TabbedRenderer
{
protected override void DispatchDraw(Canvas canvas)
{
base.DispatchDraw(canvas);
ActionBar actionBar = activity.ActionBar;
// Do Stuff here
}
}
}
But activity.ActionBar is always null.
回答1:
You should be looking for a TabLayout, not an ActionBar. Last I checked the TabLayout is the second child in the renderer's view hierarchy so you should be able to get at it like so:
var tabLayout = (TabLayout)GetChildAt(1);
Once you have that you need to loop through the individual tabs and apply your desired font size to each tab's textview.
Helpful hint, the view hierarchy looks like this:
MsiTabbedRenderer
FormsViewPager
TabLayout
SlidingTabStrip
TabView
AppCompatImageView
AppCompatTextView
TabView
AppCompatImageView
AppCompatTextView
TabView
AppCompatImageView
AppCompatTextView
...
The method I use to generate this information is included below for your enjoyment:
public static void DebugLayout(this View self, int indent = 0)
{
// write info about me first
var indents = new string('\t', indent);
System.Diagnostics.Debug.WriteLine(indents + self.GetType().Name);
// check if I'm a view group
var vg = self as ViewGroup;
if (vg == null) return;
// enumerate my children
var children = Enumerable.Range(0, vg.ChildCount).Select(x => vg.GetChildAt(x));
// recurse on each child
foreach (var child in children)
DebugLayout(child, indent+1);
}
回答2:
For Custom tab view/page i am using the xam.Tabview (Xamarin.Forms) Component and it's working as expected. It has the support for custom header and content view.
Install Nuget: TabView Nuget
Sample: Tabview Sample
来源:https://stackoverflow.com/questions/44197406/how-to-customize-the-tabbedpages-tab-items-in-xamarin-forms